home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / contrib / bibtex.web (.txt) < prev    next >
LaTeX Document  |  1988-02-11  |  362KB  |  9,155 lines

  1. % This program is copyright (C) 1985 by Oren Patashnik; all rights reserved.
  2. % Copying of this file is authorized only if (1) you are Oren Patashnik, or if
  3. % (2) you make absolutely no changes to your copy. (The WEB system provides
  4. % for alterations via an auxiliary file; the master file should stay intact.)
  5. % See Appendix H of the WEB manual for hints on how to install this program.
  6. % Version 0.98f was released in March 1985.
  7. % Version 0.98g was released in April; it removed some system dependencies
  8. %    (introducing term_in and term_out in place of just tty, and removing
  9. %    some nonlocal goto's) and it gave context for certain parsing errors.
  10. % Version 0.98h was released in April; it patched a bug in the output
  11. %    line-breaking routine that can arise with some nonstandard style files.
  12. % Version 0.98i was released in May; its main change split up the main program
  13. %    and some procedures to help certain compilers cope with size
  14. %    limitations, among other things changing error and warning macros so
  15. %    they'd produce (much) less inline code; it also redefined the class of
  16. %    legal style-file identifiers---although this affects only the bizarre
  17. %    ones, it makes BibTeX's error messages more coherent; and it had many
  18. %    minor changes, including about a 15% speed-up on TOPS-20.
  19. % Version 0.99a was released in January 1988.  Its main changes: allowed the
  20. %    inclusion of entire .bib files (rather than just those entries
  21. %    \cited or \nocited); made the sorting algorithm stable; eliminated
  22. %    any case conversion for file names; allowed concatenation in database
  23. %    fields and string definitions; handled hyphenated names properly;
  24. %    handled accented characters properly; implemented new empty$,
  25. %    preamble$, text.length$, text.prefix$, and warning$ built-in functions;
  26. %    allowed a new cross-referencing feature; and made many minor fixes,
  27. %    including about a 40% speed-up on TOPS-20.
  28. % Version 0.99b was released in February 1988.  It changed text.length$ and
  29. %    text.prefix$ to not count braces as text characters, and it changed
  30. %    text.prefix$ to add any necessary matching right braces.
  31. % Version 0.99c was released in February 1988.  It removed two begin-end pairs
  32. %    that, for convention only, surrounded entire modules, but that elicited
  33. %    label-related complaints from some compilers.
  34. % Please report any bugs to Oren Patashnik (PATASHNIK@@SCORE.STANFORD.EDU)
  35. % Although considerable effort has been expended to make the BibTeX program
  36. % correct and reliable, no warranty is implied; the author disclaims any
  37. % obligation or liability for damages, including but not limited to
  38. % special, indirect, or consequential damages arising out of or in
  39. % connection with the use or performance of this software.
  40. % This program was written by Oren Patashnik, in consultation with Leslie
  41. % Lamport, to be used with Lamport's LaTeX document preparation system.
  42. % Some modules were taken from Knuth's TeX and TeXware with his permission.
  43. % Here is TeX material that gets inserted after \input webmac
  44. \def\hang{\hangindent 3em\indent\ignorespaces}
  45. \font\ninerm=cmr9
  46. \let\mc=\ninerm % medium caps for names like PASCAL
  47. \def\PASCAL{{\mc PASCAL}}
  48. \def\ph{{\mc PASCAL-H}}
  49. \def\<#1>{$\langle#1\rangle$}
  50. \def\section{\mathhexbox278}
  51. \def\(#1){} % this is used to make section names sort themselves better
  52. \def\9#1{} % this is used for sort keys in the index via @@:sort key}{entry@@>
  53. % Note: WEAVE will typeset an upper-case `E' in a PASCAL identifier a
  54. % bit strangely so that the `TeX' in the name of this program is typeset
  55. % correctly; if this becomes a problem remove these three lines to get
  56. % normal upper-case `E's in PASCAL identifiers
  57. \def\drop{\kern-.1667em\lower.5ex\hbox{E}\kern-.125em} % middle of TeX
  58. \catcode`E=13 \uppercase{\def E{e}}
  59. \def\\#1{\hbox{\let E=\drop\it#1\/\kern.05em}} % italic type for identifiers
  60. \font\sc=cmcsc10
  61. \def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
  62.     T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
  63. \def\LaTeX{{\rm L\kern-.36em\raise.3ex\hbox{\sc a}\kern-.15em
  64.     T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
  65. \def\title{\BibTeX\ }
  66. \def\today{\ifcase\month\or
  67.   January\or February\or March\or April\or May\or June\or
  68.   July\or August\or September\or October\or November\or December\fi
  69.   \space\number\day, \number\year}
  70. \def\topofcontents{\null\vfill
  71.  \def\titlepage{F}
  72.  \centerline{\:\titlefont The {\:\ttitlefont \BibTeX} preprocessor}
  73.  \vskip 15pt \centerline{(Version 0.99c---\today)} \vfill}
  74. \pageno=\contentspagenumber \advance\pageno by 1
  75. @* Introduction.
  76. @^documentation@>
  77. @^space savings@>
  78. @^system dependencies@>
  79. @^wizard@>
  80. @!@:BibTeX}{\BibTeX@>
  81. @!@:BibTeX documentation}{\BibTeX\ documentation@>
  82. @:LaTeX}{\LaTeX@>
  83. \BibTeX\ is a preprocessor (with elements of postprocessing as
  84. explained below) for the \LaTeX\ document-preparation system.  It
  85. handles most of the formatting decisions required to produce a
  86. reference list, outputting a \.{.bbl} file that a user can edit to add
  87. any finishing touches \BibTeX\ isn't designed to handle (in practice,
  88. such editing almost never is needed); with this file \LaTeX\ actually
  89. produces the reference list.
  90. Here's how \BibTeX\ works.  It takes as input (a)~an \.{.aux} file
  91. produced by \LaTeX\ on an earlier run; (b)~a \.{.bst} file (the style
  92. file), which specifies the general reference-list style and specifies
  93. how to format individual entries, and which is written by a style
  94. designer (called a wizard throughout this program) in a
  95. special-purpose language described in the \BibTeX\ documentation---see
  96. the file {\.{btxdoc.tex}}; and (c)~\.{.bib} file(s) constituting a
  97. database of all reference-list entries the user might ever hope to
  98. use.  \BibTeX\ chooses from the \.{.bib} file(s) only those entries
  99. specified by the \.{.aux} file (that is, those given by \LaTeX's
  100. \.{\\cite} or \.{\\nocite} commands), and creates as output a \.{.bbl}
  101. file containing these entries together with the formatting commands
  102. specified by the \.{.bst} file (\BibTeX\ also creates a \.{.blg} log
  103. file, which includes any error or warning messages, but this file
  104. isn't used by any program).  \LaTeX\ will use the \.{.bbl} file,
  105. perhaps edited by the user, to produce the reference list.
  106. Many modules of \BibTeX\ were taken from Knuth's \TeX\ and \TeX ware,
  107. with his permission.  All known system-dependent modules are marked in
  108. the index entry ``system dependencies''; Dave Fuchs helped exorcise
  109. unwanted ones.  In addition, a few modules that can be changed to make
  110. \BibTeX\ smaller are marked in the index entry ``space savings''.
  111. Megathanks to Howard Trickey, for whose suggestions future users and
  112. style writers would be eternally grateful, if only they knew.
  113. The |banner| string defined here should be changed whenever \BibTeX\
  114. gets modified.
  115. @d banner=='This is BibTeX, Version 0.99c' {printed when the program starts}
  116. @^system dependencies@>
  117. Terminal output goes to the file |term_out|, while terminal input
  118. comes from |term_in|.  On our system, these (system-dependent) files
  119. are already opened at the beginning of the program, and have the same
  120. real name.
  121. @d term_out == tty
  122. @d term_in == tty
  123. @^system dependencies@>
  124. This program uses the term |print| instead of |write| when writing on
  125. both the |log_file| and (system-dependent) |term_out| file, and it
  126. uses |trace_pr| when in |trace| mode, for which it writes on just the
  127. |log_file|.  If you want to change where either set of macros writes
  128. to, you should also change the other macros in this program for that
  129. set; each such macro begins with |print_| or |trace_pr_|.
  130. @d print(#) == begin write(log_file,#); write(term_out,#); end
  131. @d print_ln(#) == begin write_ln(log_file,#); write_ln(term_out,#); end
  132. @d print_newline == print_a_newline
  133.                 {making this a procedure saves a little space}
  134. @d trace_pr(#) == begin write(log_file,#); end
  135. @d trace_pr_ln(#) == begin write_ln(log_file,#); end
  136. @d trace_pr_newline == begin write_ln(log_file); end
  137. @<Procedures and functions for all file I/O, error messages, and such@>=
  138. procedure print_a_newline;
  139. begin
  140. write_ln(log_file);
  141. write_ln(term_out);
  142. @^debugging@>
  143. @^statistics@>
  144. Some of the code below is intended to be used only when diagnosing the
  145. strange behavior that sometimes occurs when \BibTeX\ is being
  146. installed or when system wizards are fooling around with \BibTeX\
  147. without quite knowing what they are doing. Such code will not normally
  148. be compiled; it is delimited by the codewords
  149. `$|debug|\ldots|gubed|$', with apologies to people who wish to
  150. preserve the purity of English. Similarly, there is some conditional
  151. code delimited by `$|stat|\ldots|tats|$' that is intended only for use
  152. when statistics are to be kept about \BibTeX's memory/cpu usage,
  153. and there is conditional code delimited by `$|trace|\ldots|ecart|$'
  154. that is intended to be a trace facility for use mainly when debugging
  155. \.{.bst} files.
  156. @d debug == @{        { remove the `|@{|' when debugging }
  157. @d gubed == @t@>@}    { remove the `|@}|' when debugging }
  158. @f debug == begin
  159. @f gubed == end
  160. @d stat == @{        { remove the `|@{|' when keeping statistics }
  161. @d tats == @t@>@}    { remove the `|@}|' when keeping statistics }
  162. @f stat == begin
  163. @f tats == end
  164. @d trace == @{        { remove the `|@{|' when in |trace| mode }
  165. @d ecart == @t@>@}    { remove the `|@}|' when in |trace| mode }
  166. @f trace == begin
  167. @f ecart == end
  168. @^system dependencies@>
  169. We assume that |case| statements may include a
  170. default case that applies if no matching label is found,
  171. since most \PASCAL\ compilers have plugged this hole in the language by
  172. incorporating some sort of default mechanism. For example, the \ph\
  173. compiler allows `|others|:' as a default label, and other \PASCAL s allow
  174. syntaxes like `\ignorespaces|else|\unskip' or `\\{otherwise}' or
  175. `\\{otherwise}:', etc. The definitions of |othercases| and |endcases|
  176. should be changed to agree with local conventions.   Note that no semicolon
  177. appears before |endcases| in this program, so the definition of |endcases|
  178. should include a semicolon if the compiler wants one.  (Of course, if no
  179. default mechanism is available, the |case| statements of \BibTeX\ will have
  180. to be laboriously extended by listing all remaining cases. People who are
  181. stuck with such \PASCAL s have in fact done this, successfully but not
  182. happily!)
  183. @d othercases == others:    {default for cases not listed explicitly}
  184. @d endcases == @+end {follows the default case in an extended |case| statement}
  185. @f othercases == else
  186. @f endcases == end
  187. Labels are given symbolic names by the following definitions, so that
  188. occasional |goto| statements will be meaningful.  We insert the label
  189. `|exit|:' just before the `\ignorespaces|end|\unskip' of a procedure
  190. in which we have used the `|return|' statement defined below (and this
  191. is the only place `|exit|:' appears).  This label is sometimes used
  192. for exiting loops that are set up with the |loop| construction defined
  193. below.  Another generic label is `|loop_exit|:'; it appears
  194. immediately after a loop.
  195. Incidentally, this program never declares a label that isn't actually used,
  196. because some fussy \PASCAL\ compilers will complain about redundant labels.
  197. @d exit=10        {go here to leave a procedure}
  198. @d loop_exit=15        {go here to leave a loop within a procedure}
  199. @d loop1_exit=16    {the first generic label for a procedure with two}
  200. @d loop2_exit=17    {the second}
  201. @^for loops@>
  202. And |while| we're discussing loops: This program makes into |while|
  203. loops many that would otherwise be |for| loops because of Standard
  204. \PASCAL\ limitations (it's a bit complicated---standard \PASCAL\
  205. doesn't allow a global variable as the index of a |for| loop inside a
  206. procedure; furthermore, many compilers have fairly severe limitations
  207. on the size of a block, including the main block of the program; so
  208. most of the code in this program occurs inside procedures, and since
  209. for other reasons this program must use primarily global variables, it
  210. doesn't use many |for| loops).
  211. @^program conventions@>
  212. This program uses this convention: If there are several quantities in
  213. a boolean expression, they are ordered by expected frequency (except
  214. perhaps when an error message results) so that execution will be
  215. fastest; this is more an attempt to understand the program than to
  216. make it faster.
  217. Here are some macros for common programming idioms.
  218. @d incr(#) == #:=#+1    {increase a variable by unity}
  219. @d decr(#) == #:=#-1    {decrease a variable by unity}
  220. @d loop == @+ while true do@+    {repeat over and over until a |goto| happens}
  221. @f loop == xclause
  222.   {\.{WEB}'s |xclause| acts like `\ignorespaces|while true do|\unskip'}
  223. @d do_nothing ==    {empty statement}
  224. @d return == goto exit    {terminate a procedure call}
  225. @f return == nil
  226. @d empty=0        {symbolic name for a null constant}
  227. @d any_value=0        {this appeases \PASCAL's boolean-evaluation scheme}
  228. @* The main program.
  229. @^system dependencies@>
  230. @:LaTeX}{\LaTeX@>
  231. This program first reads the \.{.aux} file that \LaTeX\ produces,
  232. (\romannumeral1) determining which \.{.bib} file(s) and \.{.bst} file
  233. to read and (\romannumeral2) constructing a list of cite keys in order
  234. of occurrence.  The \.{.aux} file may have other \.{.aux} files nested
  235. within.  Second, it reads and executes the \.{.bst} file,
  236. (\romannumeral1) determining how and in which order to process the
  237. database entries in the \.{.bib} file(s) corresponding to those cite
  238. keys in the list (or in some cases, to all the entries in the \.{.bib}
  239. file(s)), (\romannumeral2) determining what text to be output for each
  240. entry and determining any additional text to be output, and
  241. (\romannumeral3) actually outputting this text to the \.{.bbl} file.
  242. In addition, the program sends error messages and other remarks to the
  243. |log_file| and terminal.
  244. @d close_up_shop=9998        {jump here after fatal errors}
  245. @d exit_program=9999        {jump here if we couldn't even get started}
  246. @t\4@>@<Compiler directives@>@/
  247. program BibTEX;            {all files are opened dynamically}
  248. label    close_up_shop,@!exit_program @<Labels in the outer block@>;
  249. const    @<Constants in the outer block@>
  250. type    @<Types in the outer block@>
  251. var    @<Globals in the outer block@>@;
  252. @<Procedures and functions for about everything@>@;
  253. @<The procedure |initialize|@>
  254. begin
  255. initialize;
  256. print_ln(banner);@/
  257. @<Read the \.{.aux} file@>;
  258. @<Read and execute the \.{.bst} file@>;
  259. close_up_shop:
  260. @<Clean up and leave@>;
  261. exit_program:
  262. @^overflow in arithmetic@>
  263. @^system dependencies@>
  264. If the first character of a \PASCAL\ comment is a dollar sign,
  265. \ph\ treats the comment as a list of ``compiler directives'' that will
  266. affect the translation of this program into machine language.  The
  267. directives shown below specify full checking and inclusion of the \PASCAL\
  268. debugger when \BibTeX\ is being debugged,
  269. but they cause range checking and other
  270. redundant code to be eliminated when the production system is being generated.
  271. Arithmetic overflow will be detected in all cases.
  272. @<Compiler directives@>=
  273. @{@&$C-,A+,D-@}     {no range check, catch arithmetic overflow, no debug overhead}
  274. @!debug @{@&$C+,D+@}@+ gubed        {but turn everything on when debugging}
  275. @^bottom up@>
  276. @^gymnastics@>
  277. @^mooning@>
  278. All procedures in this program (except for |initialize|) are grouped
  279. into one of the seven classes below, and these classes are dispersed
  280. throughout the program.  However: Much of this program is written top
  281. down, yet \PASCAL\ wants its procedures bottom up.  Since mooning is
  282. neither a technically nor a socially acceptable solution to the
  283. bottom-up problem, this section instead performs the topological
  284. gymnastics that \.{WEB} allows, ordering these classes to satisfy
  285. \PASCAL\ compilers.  There are a few procedures still out of place
  286. after this ordering, though, and the other modules that complete the
  287. task have ``gymnastics'' as an index entry.
  288. @<Procedures and functions for about everything@>=
  289. @<Procedures and functions for all file I/O, error messages, and such@>@;
  290. @<Procedures and functions for file-system interacting@>@;
  291. @<Procedures and functions for handling numbers, characters, and strings@>@;
  292. @<Procedures and functions for input scanning@>@;
  293. @<Procedures and functions for name-string processing@>@;
  294. @<Procedures and functions for style-file function execution@>@;
  295. @<Procedures and functions for the reading and processing of input files@>
  296. This procedure gets things started properly.
  297. @<The procedure |initialize|@>=
  298. procedure initialize;
  299. var @<Local variables for initialization@>
  300. begin
  301. @<Check the ``constant'' values for consistency@>;
  302. if (bad > 0) then
  303.     begin
  304.     write_ln (term_out,bad:0,' is a bad bad');
  305.     goto exit_program;
  306.     end;
  307. @<Set initial values of key variables@>;
  308. pre_def_certain_strings;@/
  309. get_the_top_level_aux_file_name;
  310. @^space savings@>
  311. @^system dependencies@>
  312. These parameters can be changed at compile time to extend or reduce
  313. \BibTeX's capacity.  They are set to accommodate about 750 cites when
  314. used with the standard styles, although |pool_size| is usually the
  315. first limitation to be a problem, often when there are 500 cites.
  316. @<Constants in the outer block@>=
  317. @!buf_size=1000; {maximum number of characters in an input line (or string)}
  318. @!min_print_line=3; {minimum \.{.bbl} line length: must be |>=3|}
  319. @!max_print_line=79; {the maximum: must be |>min_print_line| and |<buf_size|}
  320. @!aux_stack_size=20; {maximum number of simultaneous open \.{.aux} files}
  321. @!max_bib_files=20; {maximum number of \.{.bib} files allowed}
  322. @!pool_size=65000; {maximum number of characters in strings}
  323. @!max_strings=4000; {maximum number of strings, including pre-defined;
  324.                             must be |<=hash_size|}
  325. @!max_cites=750; {maximum number of distinct cite keys; must be
  326.                             |<=max_strings|}
  327. @!min_crossrefs=2; {minimum number of cross-refs required for automatic
  328.                             |cite_list| inclusion}
  329. @!wiz_fn_space=3000; {maximum amount of |wiz_defined|-function space}
  330. @!single_fn_space=100; {maximum amount for a single |wiz_defined|-function}
  331. @!max_ent_ints=3000; {maximum number of |int_entry_var|s
  332.                     (entries $\times$ |int_entry_var|s)}
  333. @!max_ent_strs=3000; {maximum number of |str_entry_var|s
  334.                     (entries $\times$ |str_entry_var|s)}
  335. @!ent_str_size=100; {maximum size of a |str_entry_var|; must be |<=buf_size|}
  336. @!glob_str_size=1000; {maximum size of a |str_global_var|;
  337.                             must be |<=buf_size|}
  338. @!max_fields=17250; {maximum number of fields (entries $\times$ fields,
  339.                     about |23*max_cites| for consistency)}
  340. @!lit_stk_size=100; {maximum number of literal functions on the stack}
  341. @^space savings@>
  342. @^system dependencies@>
  343. These parameters can also be changed at compile time, but they're
  344. needed to define some \.{WEB} numeric macros so they must be so
  345. defined themselves.
  346. @d hash_size=5000    {must be |>= max_strings| and |>= hash_prime|}
  347. @d hash_prime=4253    {a prime number about 85\% of |hash_size| and |>= 128|
  348.                         and |< @t$2^{14}-2^6$@>|}
  349. @d file_name_size=40    {file names shouldn't be longer than this}
  350. @d max_glob_strs=10    {maximum number of |str_global_var| names}
  351. @d max_glb_str_minus_1 = max_glob_strs-1  {to avoid wasting a |str_global_var|}
  352. In case somebody has inadvertently made bad settings of the ``constants,''
  353. \BibTeX\ checks them using a global variable called |bad|.
  354. This is the first of many sections of \BibTeX\ where global variables are
  355. defined.
  356. @<Globals in the outer block@>=
  357. @!bad:integer;        {is some ``constant'' wrong?}
  358. Each digit-value of |bad| has a specific meaning.
  359. @<Check the ``constant'' values for consistency@>=
  360. bad := 0;
  361. if (min_print_line < 3) then            bad:=1;
  362. if (max_print_line <= min_print_line) then    bad:=10*bad+2;
  363. if (max_print_line >= buf_size) then        bad:=10*bad+3;
  364. if (hash_prime < 128) then            bad:=10*bad+4;
  365. if (hash_prime > hash_size) then        bad:=10*bad+5;
  366. if (hash_prime >= (16384-64)) then        bad:=10*bad+6;
  367. if (max_strings > hash_size) then        bad:=10*bad+7;
  368. if (max_cites > max_strings) then        bad:=10*bad+8;
  369. if (ent_str_size > buf_size) then        bad:=10*bad+9;
  370. if (glob_str_size > buf_size) then        bad:=100*bad+11;
  371.                             {well, almost each}
  372. A global variable called |history| will contain one of four values at
  373. the end of every run: |spotless| means that no unusual messages were
  374. printed; |warning_message| means that a message of possible interest
  375. was printed but no serious errors were detected; |error_message| means
  376. that at least one error was found; |fatal_message| means that the
  377. program terminated abnormally. The value of |history| does not
  378. influence the behavior of the program; it is simply computed for the
  379. convenience of systems that might want to use such information.
  380. @d spotless=0        {|history| value for normal jobs}
  381. @d warning_message=1    {|history| value when non-serious info was printed}
  382. @d error_message=2    {|history| value when an error was noted}
  383. @d fatal_message=3    {|history| value when we had to stop prematurely}
  384. @<Procedures and functions for all file I/O, error messages, and such@>=
  385. procedure mark_warning;
  386. begin
  387. if (history = warning_message) then
  388.     incr(err_count)
  389.   else if (history = spotless) then
  390.     begin
  391.     history := warning_message;
  392.     err_count := 1;
  393.     end;
  394. procedure mark_error;
  395. begin
  396. if (history < error_message) then
  397.     begin
  398.     history := error_message;
  399.     err_count := 1;
  400.     end
  401.   else    {|history = error_message|}
  402.     incr(err_count);
  403. procedure mark_fatal;
  404. begin
  405. history := fatal_message;
  406. For the two states |warning_message| and |error_message| we keep track
  407. of the number of messages given; but since |warning_message|s aren't
  408. so serious, we ignore them once we've seen an |error_message|.  Hence
  409. we need just the single variable |err_count| to keep track.
  410. @<Globals in the outer block@>=
  411. @!history:spotless..fatal_message; {how bad was this run?}
  412. @!err_count:integer;
  413. The |err_count| gets set or reset when |history| first changes to
  414. |warning_message| or |error_message|, so we don't need to initialize
  415. @<Set initial values of key variables@>=
  416. history := spotless;
  417. @* The character set.
  418. @^ASCII code@>
  419. (The following material is copied (almost) verbatim from \TeX.
  420. Thus, the same system-dependent changes should be made to both programs.)
  421. In order to make \TeX\ readily portable between a wide variety of
  422. computers, all of its input text is converted to an internal seven-bit
  423. code that is essentially standard ASCII, the ``American Standard Code for
  424. Information Interchange.''  This conversion is done immediately when each
  425. character is read in. Conversely, characters are converted from ASCII to
  426. the user's external representation just before they are output to a
  427. text file.
  428. Such an internal code is relevant to users of \TeX\ primarily because it
  429. governs the positions of characters in the fonts. For example, the
  430. character `\.A' has ASCII code $65=@'101$, and when \TeX\ typesets
  431. this letter it specifies character number 65 in the current font.
  432. If that font actually has `\.A' in a different position, \TeX\ doesn't
  433. know what the real position is; the program that does the actual printing from
  434. \TeX's device-independent files is responsible for converting from ASCII to
  435. a particular font encoding.
  436. \TeX's internal code is relevant also with respect to constants
  437. that begin with a reverse apostrophe.
  438. Characters of text that have been converted to \TeX's internal form
  439. are said to be of type |ASCII_code|, which is a subrange of the integers.
  440. @<Types in the outer block@>=
  441. @!ASCII_code=0..127;    {seven-bit numbers}
  442. @^character set dependencies@>
  443. @^system dependencies@>
  444. The original \PASCAL\ compiler was designed in the late 60s, when six-bit
  445. character sets were common, so it did not make provision for lower-case
  446. letters. Nowadays, of course, we need to deal with both capital and small
  447. letters in a convenient way, especially in a program for typesetting;
  448. so the present specification of \TeX\ has been written under the assumption
  449. that the \PASCAL\ compiler and run-time system permit the use of text files
  450. with more than 64 distinguishable characters. More precisely, we assume that
  451. the character set contains at least the letters and symbols associated
  452. with ASCII codes @'40 through @'176; all of these characters are now
  453. available on most computer terminals.
  454. Since we are dealing with more characters than were present in the first
  455. \PASCAL\ compilers, we have to decide what to call the associated data
  456. type. Some \PASCAL s use the original name |char| for the
  457. characters in text files, even though there now are more than 64 such
  458. characters, while other \PASCAL s consider |char| to be a 64-element
  459. subrange of a larger data type that has some other name.
  460. In order to accommodate this difference, we shall use the name |text_char|
  461. to stand for the data type of the characters that are converted to and
  462. from |ASCII_code| when they are input and output. We shall also assume
  463. that |text_char| consists of the elements |chr(first_text_char)| through
  464. |chr(last_text_char)|, inclusive. The following definitions should be
  465. adjusted if necessary.
  466. @d text_char == char    {the data type of characters in text files}
  467. @d first_text_char=0    {ordinal number of the smallest element of |text_char|}
  468. @d last_text_char=127    {ordinal number of the largest element of |text_char|}
  469. @<Local variables for initialization@>=
  470. i:0..last_text_char;    {this is the first one declared}
  471. The \TeX\ processor converts between ASCII code and
  472. the user's external character set by means of arrays |xord| and |xchr|
  473. that are analogous to \PASCAL's |ord| and |chr| functions.
  474. @<Globals in the outer block@>=
  475. @!xord: array [text_char] of ASCII_code;
  476.   {specifies conversion of input characters}
  477. @!xchr: array [ASCII_code] of text_char;
  478.   {specifies conversion of output characters}
  479. @^character set dependencies@>
  480. @^system dependencies@>
  481. Since we are assuming that our \PASCAL\ system is able to read and write the
  482. visible characters of standard ASCII (although not necessarily using the
  483. ASCII codes to represent them), the following assignment statements initialize
  484. most of the |xchr| array properly, without needing any system-dependent
  485. changes. On the other hand, it is possible to implement \TeX\ with
  486. less complete character sets, and in such cases it will be necessary to
  487. change something here.
  488. @<Set initial values of key variables@>=
  489. xchr[@'40]:=' ';
  490. xchr[@'41]:='!';
  491. xchr[@'42]:='"';
  492. xchr[@'43]:='#';
  493. xchr[@'44]:='$';
  494. xchr[@'45]:='%';
  495. xchr[@'46]:='&';
  496. xchr[@'47]:='''';@/
  497. xchr[@'50]:='(';
  498. xchr[@'51]:=')';
  499. xchr[@'52]:='*';
  500. xchr[@'53]:='+';
  501. xchr[@'54]:=',';
  502. xchr[@'55]:='-';
  503. xchr[@'56]:='.';
  504. xchr[@'57]:='/';@/
  505. xchr[@'60]:='0';
  506. xchr[@'61]:='1';
  507. xchr[@'62]:='2';
  508. xchr[@'63]:='3';
  509. xchr[@'64]:='4';
  510. xchr[@'65]:='5';
  511. xchr[@'66]:='6';
  512. xchr[@'67]:='7';@/
  513. xchr[@'70]:='8';
  514. xchr[@'71]:='9';
  515. xchr[@'72]:=':';
  516. xchr[@'73]:=';';
  517. xchr[@'74]:='<';
  518. xchr[@'75]:='=';
  519. xchr[@'76]:='>';
  520. xchr[@'77]:='?';@/
  521. xchr[@'100]:='@@';
  522. xchr[@'101]:='A';
  523. xchr[@'102]:='B';
  524. xchr[@'103]:='C';
  525. xchr[@'104]:='D';
  526. xchr[@'105]:='E';
  527. xchr[@'106]:='F';
  528. xchr[@'107]:='G';@/
  529. xchr[@'110]:='H';
  530. xchr[@'111]:='I';
  531. xchr[@'112]:='J';
  532. xchr[@'113]:='K';
  533. xchr[@'114]:='L';
  534. xchr[@'115]:='M';
  535. xchr[@'116]:='N';
  536. xchr[@'117]:='O';@/
  537. xchr[@'120]:='P';
  538. xchr[@'121]:='Q';
  539. xchr[@'122]:='R';
  540. xchr[@'123]:='S';
  541. xchr[@'124]:='T';
  542. xchr[@'125]:='U';
  543. xchr[@'126]:='V';
  544. xchr[@'127]:='W';@/
  545. xchr[@'130]:='X';
  546. xchr[@'131]:='Y';
  547. xchr[@'132]:='Z';
  548. xchr[@'133]:='[';
  549. xchr[@'134]:='\';
  550. xchr[@'135]:=']';
  551. xchr[@'136]:='^';
  552. xchr[@'137]:='_';@/
  553. xchr[@'140]:='`';
  554. xchr[@'141]:='a';
  555. xchr[@'142]:='b';
  556. xchr[@'143]:='c';
  557. xchr[@'144]:='d';
  558. xchr[@'145]:='e';
  559. xchr[@'146]:='f';
  560. xchr[@'147]:='g';@/
  561. xchr[@'150]:='h';
  562. xchr[@'151]:='i';
  563. xchr[@'152]:='j';
  564. xchr[@'153]:='k';
  565. xchr[@'154]:='l';
  566. xchr[@'155]:='m';
  567. xchr[@'156]:='n';
  568. xchr[@'157]:='o';@/
  569. xchr[@'160]:='p';
  570. xchr[@'161]:='q';
  571. xchr[@'162]:='r';
  572. xchr[@'163]:='s';
  573. xchr[@'164]:='t';
  574. xchr[@'165]:='u';
  575. xchr[@'166]:='v';
  576. xchr[@'167]:='w';@/
  577. xchr[@'170]:='x';
  578. xchr[@'171]:='y';
  579. xchr[@'172]:='z';
  580. xchr[@'173]:='{';
  581. xchr[@'174]:='|';
  582. xchr[@'175]:='}';
  583. xchr[@'176]:='~';@/
  584. xchr[0]:=' '; xchr[@'177]:=' ';
  585.   {ASCII codes 0 and |@'177| do not appear in text}
  586. @^character set dependencies@>
  587. @^system dependencies@>
  588. Some of the ASCII codes without visible characters have been given symbolic
  589. names in this program because they are used with a special meaning.  The
  590. |tab| character may be system dependent.
  591. @d null_code=@'0    {ASCII code that might disappear}
  592. @d tab=@'11        {ASCII code treated as |white_space|}
  593. @d space=@'40        {ASCII code treated as |white_space|}
  594. @d invalid_code=@'177    {ASCII code that should not appear}
  595. @^character set dependencies@>
  596. @^system dependencies@>
  597. @:TeXbook}{\sl The \TeX book@>
  598. The ASCII code is ``standard'' only to a certain extent, since many
  599. computer installations have found it advantageous to have ready access
  600. to more than 94 printing characters. Appendix~C of {\sl The \TeX book\/}
  601. gives a complete specification of the intended correspondence between
  602. characters and \TeX's internal representation.
  603. If \TeX\ is being used
  604. on a garden-variety \PASCAL\ for which only standard ASCII
  605. codes will appear in the input and output files, it doesn't really matter
  606. what codes are specified in |xchr[1..@'37]|, but the safest policy is to
  607. blank everything out by using the code shown below.
  608. However, other settings of |xchr| will make \TeX\ more friendly on
  609. computers that have an extended character set, so that users can type things
  610. like `\.^^Z' instead of `\.{\\ne}'. At MIT, for example, it would be more
  611. appropriate to substitute the code
  612. $$\hbox{|for i:=1 to @'37 do xchr[i]:=chr(i);|}$$
  613. \TeX's character set is essentially the same as MIT's, even with respect to
  614. characters less than~@'40. People with extended character sets can
  615. assign codes arbitrarily, giving an |xchr| equivalent to whatever
  616. characters the users of \TeX\ are allowed to have in their input files.
  617. It is best to make the codes correspond to the intended interpretations as
  618. shown in Appendix~C whenever possible; but this is not necessary. For
  619. example, in countries with an alphabet of more than 26 letters, it is
  620. usually best to map the additional letters into codes less than~@'40.
  621. @<Set initial values of key variables@>=
  622. for i:=1 to @'37 do xchr[i]:=' ';
  623. xchr[tab]:=chr(tab);
  624. This system-independent code makes the |xord| array contain a suitable
  625. inverse to the information in |xchr|. Note that if |xchr[i]=xchr[j]|
  626. where |i<j<@'177|, the value of |xord[xchr[i]]| will turn out to be
  627. |j| or more; hence, standard ASCII code numbers will be used instead
  628. of codes below @'40 in case there is a coincidence.
  629. @<Set initial values of key variables@>=
  630. for i:=first_text_char to last_text_char do xord[chr(i)]:=invalid_code;
  631. for i:=1 to @'176 do xord[xchr[i]]:=i;
  632. Also, various characters are given symbolic names; all the ones this
  633. program uses are collected here.  We use the sharp sign as the
  634. |concat_char|, rather than something more natural (like an ampersand),
  635. for uniformity of database syntax (ampersand is a valid character in
  636. identifiers).
  637. @d double_quote = """"        {delimits strings}
  638. @d number_sign = "#"        {marks an |int_literal|}
  639. @d comment = "%"        {ignore the rest of a \.{.bst} or \TeX\ line}
  640. @d single_quote = "'"        {marks a quoted function}
  641. @d left_paren = "("        {optional database entry left delimiter}
  642. @d right_paren = ")"        {corresponding right delimiter}
  643. @d comma = ","            {separates various things}
  644. @d minus_sign = "-"        {for a negative number}
  645. @d equals_sign = "="        {separates a field name from a field value}
  646. @d at_sign = "@@"        {the beginning of a database entry}
  647. @d left_brace = "{"        {left delimiter of many things}
  648. @d right_brace = "}"        {corresponding right delimiter}
  649. @d period = "."            {these are three}
  650. @d question_mark = "?"        {string-ending characters}
  651. @d exclamation_mark = "!"    {of interest in \.{add.period\$}}
  652. @d tie = "~"            {the default space char, in \.{format.name\$}}
  653. @d hyphen = "-"            {like |white_space|, in \.{format.name\$}}
  654. @d star = "*"            {for including entire database}
  655. @d concat_char = "#"        {for concatenating field tokens}
  656. @d colon = ":"            {for lower-casing (usually title) strings}
  657. @d backslash = "\"        {used to recognize accented characters}
  658. These arrays give a lexical classification for the |ASCII_code|s;
  659. |lex_class| is used for general scanning and |id_class| is used for
  660. scanning identifiers.
  661. @<Globals in the outer block@>=
  662. @!lex_class: array [ASCII_code] of lex_type;
  663. @!id_class: array [ASCII_code] of id_type;
  664. Every character has two types of the lexical classifications.  The
  665. first type is general, and the second type tells whether the character
  666. is legal in identifiers.
  667. @d illegal = 0        {the unrecognized |ASCII_code|s}
  668. @d white_space = 1    {things like |space|s that you can't see}
  669. @d alpha = 2        {the upper- and lower-case letters}
  670. @d numeric = 3        {the ten digits}
  671. @d sep_char = 4        {things sometimes treated like |white_space|}
  672. @d other_lex = 5    {when none of the above applies}
  673. @d last_lex = 5        {the same number as on the line above}
  674. @d illegal_id_char = 0    {a few forbidden ones}
  675. @d legal_id_char = 1    {most printing characters}
  676. @<Types in the outer block@>=
  677. @!lex_type = 0..last_lex;@/
  678. @!id_type = 0..1;
  679. @^character set dependencies@>
  680. @^system dependencies@>
  681. Now we initialize the system-dependent |lex_class| array.  The |tab|
  682. character may be system dependent.  Note that the order of these
  683. assignments is important here.
  684. @<Set initial values of key variables@>=
  685. for i:=0 to @'177 do lex_class[i] := other_lex;
  686. for i:=0 to @'37 do lex_class[i] := illegal;
  687. lex_class[invalid_code] := illegal;
  688. lex_class[tab] := white_space;
  689. lex_class[space] := white_space;
  690. lex_class[tie] := sep_char;
  691. lex_class[hyphen] := sep_char;
  692. for i:=@'60 to @'71 do lex_class[i] := numeric;
  693. for i:=@'101 to @'132 do lex_class[i] := alpha;
  694. for i:=@'141 to @'172 do lex_class[i] := alpha;
  695. @^character set dependencies@>
  696. @^system dependencies@>
  697. And now the |id_class| array.
  698. @<Set initial values of key variables@>=
  699. for i:=0 to @'177 do id_class[i] := legal_id_char;
  700. for i:=0 to @'37 do id_class[i] := illegal_id_char;
  701. id_class[space] := illegal_id_char;
  702. id_class[tab] := illegal_id_char;
  703. id_class[double_quote] := illegal_id_char;
  704. id_class[number_sign] := illegal_id_char;
  705. id_class[comment] := illegal_id_char;
  706. id_class[single_quote] := illegal_id_char;
  707. id_class[left_paren] := illegal_id_char;
  708. id_class[right_paren] := illegal_id_char;
  709. id_class[comma] := illegal_id_char;
  710. id_class[equals_sign] := illegal_id_char;
  711. id_class[left_brace] := illegal_id_char;
  712. id_class[right_brace] := illegal_id_char;
  713. The array |char_width| gives relative printing widths of each
  714. |ASCII_code|, and |string_width| will be used later to sum up
  715. |char_width|s in a string.
  716. @<Globals in the outer block@>=
  717. @!char_width : array [ASCII_code] of integer;
  718. @!string_width : integer;
  719. @^character set dependencies@>
  720. @^system dependencies@>
  721. Now we initialize the system-dependent |char_width| array, for which
  722. |space| is the only |white_space| character given a nonzero printing
  723. width.  The widths here are taken from Stanford's June~'87
  724. $cmr10$~font and represent hundredths of a point (rounded), but since
  725. they're used only for relative comparisons, the units have no meaning.
  726. @d ss_width = 500        {character |@'31|'s width in the $cmr10$ font}
  727. @d ae_width = 722        {character |@'32|'s width in the $cmr10$ font}
  728. @d oe_width = 778        {character |@'33|'s width in the $cmr10$ font}
  729. @d upper_ae_width = 903        {character |@'35|'s width in the $cmr10$ font}
  730. @d upper_oe_width = 1014    {character |@'36|'s width in the $cmr10$ font}
  731. @<Set initial values of key variables@>=
  732. for i:=0 to @'177 do char_width[i] := 0;
  733. char_width[@'40] := 278;
  734. char_width[@'41] := 278;
  735. char_width[@'42] := 500;
  736. char_width[@'43] := 833;
  737. char_width[@'44] := 500;
  738. char_width[@'45] := 833;
  739. char_width[@'46] := 778;
  740. char_width[@'47] := 278;
  741. char_width[@'50] := 389;
  742. char_width[@'51] := 389;
  743. char_width[@'52] := 500;
  744. char_width[@'53] := 778;
  745. char_width[@'54] := 278;
  746. char_width[@'55] := 333;
  747. char_width[@'56] := 278;
  748. char_width[@'57] := 500;
  749. char_width[@'60] := 500;
  750. char_width[@'61] := 500;
  751. char_width[@'62] := 500;
  752. char_width[@'63] := 500;
  753. char_width[@'64] := 500;
  754. char_width[@'65] := 500;
  755. char_width[@'66] := 500;
  756. char_width[@'67] := 500;
  757. char_width[@'70] := 500;
  758. char_width[@'71] := 500;
  759. char_width[@'72] := 278;
  760. char_width[@'73] := 278;
  761. char_width[@'74] := 278;
  762. char_width[@'75] := 778;
  763. char_width[@'76] := 472;
  764. char_width[@'77] := 472;
  765. char_width[@'100] := 778;
  766. char_width[@'101] := 750;
  767. char_width[@'102] := 708;
  768. char_width[@'103] := 722;
  769. char_width[@'104] := 764;
  770. char_width[@'105] := 681;
  771. char_width[@'106] := 653;
  772. char_width[@'107] := 785;
  773. char_width[@'110] := 750;
  774. char_width[@'111] := 361;
  775. char_width[@'112] := 514;
  776. char_width[@'113] := 778;
  777. char_width[@'114] := 625;
  778. char_width[@'115] := 917;
  779. char_width[@'116] := 750;
  780. char_width[@'117] := 778;
  781. char_width[@'120] := 681;
  782. char_width[@'121] := 778;
  783. char_width[@'122] := 736;
  784. char_width[@'123] := 556;
  785. char_width[@'124] := 722;
  786. char_width[@'125] := 750;
  787. char_width[@'126] := 750;
  788. char_width[@'127] :=1028;
  789. char_width[@'130] := 750;
  790. char_width[@'131] := 750;
  791. char_width[@'132] := 611;
  792. char_width[@'133] := 278;
  793. char_width[@'134] := 500;
  794. char_width[@'135] := 278;
  795. char_width[@'136] := 500;
  796. char_width[@'137] := 278;
  797. char_width[@'140] := 278;
  798. char_width[@'141] := 500;
  799. char_width[@'142] := 556;
  800. char_width[@'143] := 444;
  801. char_width[@'144] := 556;
  802. char_width[@'145] := 444;
  803. char_width[@'146] := 306;
  804. char_width[@'147] := 500;
  805. char_width[@'150] := 556;
  806. char_width[@'151] := 278;
  807. char_width[@'152] := 306;
  808. char_width[@'153] := 528;
  809. char_width[@'154] := 278;
  810. char_width[@'155] := 833;
  811. char_width[@'156] := 556;
  812. char_width[@'157] := 500;
  813. char_width[@'160] := 556;
  814. char_width[@'161] := 528;
  815. char_width[@'162] := 392;
  816. char_width[@'163] := 394;
  817. char_width[@'164] := 389;
  818. char_width[@'165] := 556;
  819. char_width[@'166] := 528;
  820. char_width[@'167] := 722;
  821. char_width[@'170] := 528;
  822. char_width[@'171] := 528;
  823. char_width[@'172] := 444;
  824. char_width[@'173] := 500;
  825. char_width[@'174] :=1000;
  826. char_width[@'175] := 500;
  827. char_width[@'176] := 500;
  828. @* Input and output.
  829. The basic operations we need to do are
  830. (1)~inputting and outputting of text characters to or from a file;
  831. (2)~instructing the operating system to initiate (``open'')
  832. or to terminate (``close'') input or output to or from a specified file; and
  833. (3)~testing whether the end of an input file has been reached.
  834. @<Types in the outer block@>=
  835. @!alpha_file=packed file of text_char;    {files that contain textual data}
  836. @^system dependencies@>
  837. Most of what we need to do with respect to input and output can be handled
  838. by the I/O facilities that are standard in \PASCAL, i.e., the routines
  839. called |get|, |put|, |eof|, and so on. But
  840. standard \PASCAL\ does not allow file variables to be associated with file
  841. names that are determined at run time, so it cannot be used to implement
  842. \BibTeX; some sort of extension to \PASCAL's ordinary |reset| and |rewrite|
  843. is crucial for our purposes. We shall assume that |name_of_file| is a variable
  844. of an appropriate type such that the \PASCAL\ run-time system being used to
  845. implement \BibTeX\ can open a file whose external name is specified by
  846. |name_of_file|. \BibTeX\ does no case conversion for file names.
  847. @<Globals in the outer block@>=
  848. @!name_of_file:packed array[1..file_name_size] of char;
  849.              {on some systems this is a \&{record} variable}
  850. @!name_length:0..file_name_size;
  851.   {this many characters are relevant in |name_of_file| (the rest are blank)}
  852. @!name_ptr:0..file_name_size+1;        {index variable into |name_of_file|}
  853. @^system dependencies@>
  854. @:PASCAL H}{\ph@>
  855. The \ph\ compiler with which the present version of \TeX\ was prepared has
  856. extended the rules of \PASCAL\ in a very convenient way. To open file~|f|,
  857. we can write
  858. $$\vbox{\halign{#\hfil\qquad&#\hfil\cr
  859. |reset(f,@t\\{name}@>,'/O')|&for input;\cr
  860. |rewrite(f,@t\\{name}@>,'/O')|&for output.\cr}}$$
  861. The `\\{name}' parameter, which is of type `\ignorespaces|packed
  862. array[@t\<\\{any}>@>] of text_char|', stands for the name of
  863. the external file that is being opened for input or output.
  864. Blank spaces that might appear in \\{name} are ignored.
  865. The `\.{/O}' parameter tells the operating system not to issue its own
  866. error messages if something goes wrong. If a file of the specified name
  867. cannot be found, or if such a file cannot be opened for some other reason
  868. (e.g., someone may already be trying to write the same file), we will have
  869. |@!erstat(f)<>0| after an unsuccessful |reset| or |rewrite|.  This allows
  870. \TeX\ to undertake appropriate corrective action.
  871. \TeX's file-opening procedures return |false| if no file identified by
  872. |name_of_file| could be opened.
  873. @d reset_OK(#)==erstat(#)=0
  874. @d rewrite_OK(#)==erstat(#)=0
  875. @<Procedures and functions for file-system interacting@>=
  876. function erstat(var f:file):integer; extern;    {in the runtime library}
  877. @#@t\2@>
  878. function a_open_in(var f:alpha_file):boolean;    {open a text file for input}
  879. begin reset(f,name_of_file,'/O'); a_open_in:=reset_OK(f);
  880. function a_open_out(var f:alpha_file):boolean;    {open a text file for output}
  881. begin rewrite(f,name_of_file,'/O'); a_open_out:=rewrite_OK(f);
  882. @^system dependencies@>
  883. Files can be closed with the \ph\ routine `|close(f)|', which should
  884. be used when all input or output with respect to |f| has been
  885. completed.  This makes |f| available to be opened again, if desired;
  886. and if |f| was used for output, the |close| operation makes the
  887. corresponding external file appear on the user's area, ready to be
  888. read.
  889. @<Procedures and functions for file-system interacting@>=
  890. procedure a_close(var f:alpha_file);        {close a text file}
  891. begin close(f);
  892. Text output is easy to do with the ordinary \PASCAL\ |put| procedure,
  893. so we don't have to make any other special arrangements.
  894. The treatment of text input is more difficult, however, because
  895. of the necessary translation to |ASCII_code| values, and because
  896. \TeX's conventions should be efficient and they should
  897. blend nicely with the user's operating environment.
  898. Input from text files is read one line at a time, using a routine
  899. called |input_ln|. This function is defined in terms of global
  900. variables called |buffer| and |last|.  The |buffer| array contains
  901. |ASCII_code| values, and |last| is an index into this array marking
  902. the end of a line of text.  (Occasionally, |buffer| is used for
  903. something else, in which case it is copied to a temporary array.)
  904. @<Globals in the outer block@>=
  905. @!buffer:buf_type;    {usually, lines of characters being read}
  906. @!last:buf_pointer;    {end of the line just input to |buffer|}
  907. @^save space@>
  908. @^space savings@>
  909. @^system dependencies@>
  910. The type |buf_type| is used for |buffer|, for saved copies of it, or
  911. for scratch work.  It's not |packed| because otherwise the program
  912. would run much slower on some systems (more than 25 percent slower,
  913. for example, on a TOPS-20 operating system).  But on systems that are
  914. byte-addressable and that have a good compiler, packing |buf_type|
  915. would save lots of space without much loss of speed.  Other modules
  916. that have packable arrays are also marked with a ``space savings''
  917. index entry.
  918. @<Types in the outer block@>=
  919. @!buf_pointer = 0..buf_size;            {an index into a |buf_type|}
  920. @!buf_type = array[buf_pointer] of ASCII_code;    {for various buffers}
  921. @^kludge@>
  922. And while we're at it, we declare another buffer for general use.
  923. Because buffers are not packed and can get large, we use |sv_buffer|
  924. several purposes; this is a bit kludgy, but it helps make the stack
  925. space not overflow on some machines.  It's used when reading the
  926. entire database file (in the \.{read} command) and when doing
  927. name-handling (through the alias |name_buf|) in the |built_in|
  928. functions \.{format.names\$} and \.{num.names\$}.
  929. @<Globals in the outer block@>=
  930. @!sv_buffer : buf_type;
  931. @!sv_ptr1 : buf_pointer;
  932. @!sv_ptr2 : buf_pointer;
  933. @!tmp_ptr,@!tmp_end_ptr : integer; {copy pointers only, usually for buffers}
  934. @.BibTeX capacity exceeded@>
  935. When something in the program wants to be bigger or something out
  936. there wants to be smaller, it's time to call it a run.  Here's the
  937. first of several macros that have associated procedures so that they
  938. produce less inline code.
  939. @d overflow(#)==begin        {fatal error---close up shop}
  940.         print_overflow;
  941.         print_ln(#:0);
  942.         goto close_up_shop;
  943.         end
  944. @<Procedures and functions for all file I/O, error messages, and such@>=
  945. procedure print_overflow;
  946. begin
  947. print ('Sorry---you''ve exceeded BibTeX''s ');
  948. mark_fatal;
  949. @.this can't happen@>
  950. When something happens that the program thinks is impossible,
  951. call the maintainer.
  952. @d confusion(#)==begin        {fatal error---close up shop}
  953.          print (#);
  954.          print_confusion;
  955.          goto close_up_shop;
  956.          end
  957. @<Procedures and functions for all file I/O, error messages, and such@>=
  958. procedure print_confusion;
  959. begin
  960. print_ln ('---this can''t happen');
  961. print_ln ('*Please notify the BibTeX maintainer*');
  962. mark_fatal;
  963. @:BibTeX capacity exceeded}{\quad buffer size@>
  964. When a buffer overflows, it's time to complain (and then quit).
  965. @<Procedures and functions for all file I/O, error messages, and such@>=
  966. procedure buffer_overflow;
  967. begin
  968. overflow('buffer size ',buf_size);
  969. @:BibTeX capacity exceeded}{\quad buffer size@>
  970. The |input_ln| function brings the next line of input from the
  971. specified file into available positions of the buffer array and
  972. returns the value |true|, unless the file has already been entirely
  973. read, in which case it returns |false| and sets |last:=0|.  In
  974. general, the |ASCII_code| numbers that represent the next line of the
  975. file are input into |buffer[0]|, |buffer[1]|, \dots, |buffer[last-1]|;
  976. and the global variable |last| is set equal to the length of the line.
  977. Trailing |white_space| characters are removed from the line
  978. (|white_space| characters are explained in the character-set section%
  979. ---most likely they're blanks); thus, either |last=0| (in which case
  980. the line was entirely blank) or |lex_class[buffer[last-1]]<>white_space|.
  981. An overflow error is given if the normal actions of |input_ln| would
  982. make |last>buf_size|.
  983. Standard \PASCAL\ says that a file should have |eoln| immediately
  984. before |eof|, but \BibTeX\ needs only a weaker restriction: If |eof|
  985. occurs in the middle of a line, the system function |eoln| should return
  986. a |true| result (even though |f^| will be undefined).
  987. @<Procedures and functions for all file I/O, error messages, and such@>=
  988. function input_ln(var f:alpha_file) : boolean;
  989.                 {inputs the next line or returns |false|}
  990. label loop_exit;
  991. begin
  992. last:=0;
  993. if (eof(f)) then input_ln:=false
  994.   begin
  995.   while (not eoln(f)) do
  996.     begin
  997.     if (last >= buf_size) then
  998.     buffer_overflow;
  999.     buffer[last]:=xord[f^];
  1000.     get(f); incr(last);
  1001.     end;
  1002.   get(f);
  1003.   while (last > 0) do        {remove trailing |white_space|}
  1004.     if (lex_class[buffer[last-1]] = white_space) then
  1005.       decr(last)
  1006.      else
  1007.       goto loop_exit;
  1008. loop_exit:
  1009.   input_ln:=true;
  1010.   end;
  1011. @* String handling.
  1012. \BibTeX\ uses variable-length strings of seven-bit characters.
  1013. Since \PASCAL\ does not have a well-developed string mechanism,
  1014. \BibTeX\ does all its string processing by home-grown
  1015. (predominantly \TeX's) methods.
  1016. Unlike \TeX, however, \BibTeX\ does not use a |pool_file| for
  1017. string storage; it creates its few pre-defined strings at run-time.
  1018. The necessary operations are handled with a simple data structure.
  1019. The array |str_pool| contains all the (seven-bit) ASCII codes in all
  1020. the strings \BibTeX\ must ever search for (generally identifiers
  1021. names), and the array |str_start| contains indices of the starting
  1022. points of each such string. Strings are referred to by integer
  1023. numbers, so that string number |s| comprises the characters
  1024. |str_pool[j]| for |str_start[s]<=j<str_start[s+1]|. Additional integer
  1025. variables |pool_ptr| and |str_ptr| indicate the number of entries used
  1026. so far in |str_pool| and |str_start|; locations |str_pool[pool_ptr]|
  1027. and |str_start[str_ptr]| are ready for the next string to be
  1028. allocated.  Location |str_start[0]| is unused so that hashing will
  1029. work correctly.
  1030. Elements of the |str_pool| array must be ASCII codes that can actually be
  1031. printed; i.e., they must have an |xchr| equivalent in the local
  1032. character set.
  1033. @<Globals in the outer block@>=
  1034. @!str_pool : packed array[pool_pointer] of ASCII_code;    {the characters}
  1035. @!str_start : packed array[str_number] of pool_pointer;    {the starting pointers}
  1036. @!pool_ptr : pool_pointer;    {first unused position in |str_pool|}
  1037. @!str_ptr : str_number;        {start of the current string being created}
  1038. @!str_num : str_number;        {general index variable into |str_start|}
  1039. @!p_ptr1,@!p_ptr2 : pool_pointer;    {several procedures use these locally}
  1040. Where |pool_pointer| and |str_number| are pointers into |str_pool| and
  1041. |str_start|.
  1042. @<Types in the outer block@>=
  1043. @!pool_pointer = 0..pool_size;    {for variables that point into |str_pool|}
  1044. @!str_number = 0..max_strings;    {for variables that point into |str_start|}
  1045. These macros send a string in |str_pool| to an output file.
  1046. @d max_pop = 3    {---see the |built_in| functions section}
  1047. @d print_pool_str(#) ==    print_a_pool_str(#)
  1048.                 {making this a procedure saves a little space}
  1049. @d trace_pr_pool_str(#) == begin
  1050.                out_pool_str(log_file,#);
  1051.                end
  1052. @^kludge@>
  1053. @^system dependencies@>
  1054. @:this can't happen}{\quad Illegal string number@>
  1055. And here are the associated procedures.  Note: The |term_out| file is
  1056. system dependent.
  1057. @<Procedures and functions for all file I/O, error messages, and such@>=
  1058. procedure out_pool_str (var f:alpha_file; @!s:str_number);
  1059. var i:pool_pointer;
  1060. begin    {allowing |str_ptr <= s < str_ptr+max_pop| is a \.{.bst}-stack kludge}
  1061. if ((s<0) or (s>=str_ptr+max_pop) or (s>=max_strings)) then
  1062.     confusion ('Illegal string number:',s:0);
  1063. for i := str_start[s] to str_start[s+1]-1 do
  1064.     write(f,xchr[str_pool[i]]);
  1065. procedure print_a_pool_str (@!s:str_number);
  1066. begin
  1067. out_pool_str(term_out,s);
  1068. out_pool_str(log_file,s);
  1069. @.WEB@>
  1070. Several of the elementary string operations are performed using \.{WEB}
  1071. macros instead of using \PASCAL\ procedures, because many of the
  1072. operations are done quite frequently and we want to avoid the
  1073. overhead of procedure calls. For example, here is
  1074. a simple macro that computes the length of a string.
  1075. @d length(#) == (str_start[#+1]-str_start[#])
  1076.             {the number of characters in string number \#}
  1077. @:BibTeX capacity exceeded}{\quad pool size@>
  1078. Strings are created by appending character codes to |str_pool|.
  1079. The macro called |append_char|, defined here, does not check to see if the
  1080. value of |pool_ptr| has gotten too high; this test is supposed to be
  1081. made before |append_char| is used.
  1082. To test if there is room to append |l| more characters to |str_pool|,
  1083. we shall write |str_room(l)|, which aborts \BibTeX\ and gives an
  1084. error message if there isn't enough room.
  1085. @d append_char(#) ==        {put |ASCII_code| \# at the end of |str_pool|}
  1086. begin str_pool[pool_ptr]:=#; incr(pool_ptr);
  1087. @d str_room(#) ==        {make sure that the pool hasn't overflowed}
  1088.   begin
  1089.   if (pool_ptr+# > pool_size) then
  1090.       pool_overflow;
  1091.   end
  1092. @<Procedures and functions for all file I/O, error messages, and such@>=
  1093. procedure pool_overflow;
  1094. begin
  1095. overflow('pool size ',pool_size);
  1096. @:BibTeX capacity exceeded}{\quad number of strings@>
  1097. Once a sequence of characters has been appended to |str_pool|, it
  1098. officially becomes a string when the function |make_string| is called.
  1099. It returns the string number of the string it just made.
  1100. @<Procedures and functions for handling numbers, characters, and strings@>=
  1101. function make_string : str_number;    {current string enters the pool}
  1102. begin
  1103. if (str_ptr=max_strings) then
  1104.     overflow('number of strings ',max_strings);
  1105. incr(str_ptr);
  1106. str_start[str_ptr]:=pool_ptr;
  1107. make_string := str_ptr - 1;
  1108. These macros destroy and recreate the string at the end of the pool.
  1109. @d flush_string == begin
  1110.            decr(str_ptr);
  1111.            pool_ptr := str_start[str_ptr];
  1112.            end
  1113. @d unflush_string == begin
  1114.              incr(str_ptr);
  1115.              pool_ptr := str_start[str_ptr];
  1116.              end
  1117. This subroutine compares string |s| with another string that appears
  1118. in the buffer |buf| between positions |bf_ptr| and |bf_ptr+len-1|; the
  1119. result is |true| if and only if the strings are equal.
  1120. @<Procedures and functions for handling numbers, characters, and strings@>=
  1121. function str_eq_buf (@!s:str_number; var buf:buf_type;
  1122.                     @!bf_ptr,@!len:buf_pointer) : boolean;
  1123.   {test equality of strings}
  1124. label exit;
  1125. var i : buf_pointer;    {running}
  1126. @!j : pool_pointer;    {indices}
  1127. begin
  1128. if (length(s) <> len) then    {strings of unequal length}
  1129.     begin
  1130.     str_eq_buf := false;
  1131.     return;
  1132.     end;
  1133. i := bf_ptr;
  1134. j := str_start[s];
  1135. while (j < str_start[s+1]) do
  1136.     begin
  1137.     if (str_pool[j] <> buf[i]) then
  1138.     begin
  1139.     str_eq_buf := false;
  1140.     return;
  1141.     end;
  1142.     incr(i);
  1143.     incr(j);
  1144.     end;
  1145. str_eq_buf := true;
  1146. exit:
  1147. This subroutine compares two |str_pool| strings and returns true
  1148. |true| if and only if the strings are equal.
  1149. @<Procedures and functions for handling numbers, characters, and strings@>=
  1150. function str_eq_str (@!s1,@!s2:str_number) : boolean;
  1151. label exit;
  1152. begin
  1153. if (length(s1) <> length(s2)) then
  1154.     begin
  1155.     str_eq_str := false;
  1156.     return;
  1157.     end;
  1158. p_ptr1 := str_start[s1];
  1159. p_ptr2 := str_start[s2];
  1160. while (p_ptr1 < str_start[s1+1]) do
  1161.     begin
  1162.     if (str_pool[p_ptr1] <> str_pool[p_ptr2]) then
  1163.     begin
  1164.     str_eq_str := false;
  1165.     return;
  1166.     end;
  1167.     incr(p_ptr1);
  1168.     incr(p_ptr2);
  1169.     end;
  1170. str_eq_str:=true;
  1171. exit:
  1172. @:BibTeX capacity exceeded}{\quad file name size@>
  1173. This procedure copies file name |file_name| into the beginning of
  1174. |name_of_file|, if it will fit.  It also sets the global variable
  1175. |name_length| to the appropriate value.
  1176. @<Procedures and functions for file-system interacting@>=
  1177. procedure start_name (@!file_name:str_number);
  1178. var p_ptr: pool_pointer;    {running index}
  1179. begin
  1180. if (length(file_name) > file_name_size) then
  1181.     begin
  1182.     print ('File=');
  1183.     print_pool_str (file_name);
  1184.     print_ln (',');
  1185.     file_nm_size_overflow;
  1186.     end;
  1187. name_ptr := 1;
  1188. p_ptr := str_start[file_name];
  1189. while (p_ptr < str_start[file_name+1]) do
  1190.     begin
  1191.     name_of_file[name_ptr] := chr (str_pool[p_ptr]);
  1192.     incr(name_ptr); incr(p_ptr);
  1193.     end;
  1194. name_length := length(file_name);
  1195. @:BibTeX capacity exceeded}{\quad file name size@>
  1196. Yet another complaint-before-quiting.
  1197. @<Procedures and functions for all file I/O, error messages, and such@>=
  1198. procedure file_nm_size_overflow;
  1199. begin
  1200. overflow('file name size ',file_name_size);
  1201. @:BibTeX capacity exceeded}{\quad file name size@>
  1202. This procedure copies file extension |ext| into the array
  1203. |name_of_file| starting at position |name_length+1|.  It also sets the
  1204. global variable |name_length| to the appropriate value.
  1205. @<Procedures and functions for file-system interacting@>=
  1206. procedure add_extension(@!ext:str_number);
  1207. var p_ptr: pool_pointer;    {running index}
  1208. begin
  1209. if (name_length + length(ext) > file_name_size) then
  1210.     begin
  1211.     print ('File=',name_of_file,', extension=');
  1212.     print_pool_str (ext); print_ln (',');
  1213.     file_nm_size_overflow;
  1214.     end;
  1215. name_ptr := name_length + 1;
  1216. p_ptr := str_start[ext];
  1217. while (p_ptr < str_start[ext+1]) do
  1218.     begin
  1219.     name_of_file[name_ptr] := chr (str_pool[p_ptr]);
  1220.     incr(name_ptr); incr(p_ptr);
  1221.     end;
  1222. name_length := name_length + length(ext);
  1223. name_ptr := name_length+1;
  1224. while (name_ptr <= file_name_size) do    {pad with blanks}
  1225.     begin
  1226.     name_of_file[name_ptr] := ' ';
  1227.     incr(name_ptr);
  1228.     end;
  1229. @:BibTeX capacity exceeded}{\quad file name size@>
  1230. This procedure copies the default logical area name |area| into the
  1231. array |name_of_file| starting at position 1, after shifting up the
  1232. rest of the filename.  It also sets the global variable |name_length|
  1233. to the appropriate value.
  1234. @<Procedures and functions for file-system interacting@>=
  1235. procedure add_area(@!area:str_number);
  1236. var p_ptr: pool_pointer;    {running index}
  1237. begin
  1238. if (name_length + length(area) > file_name_size) then
  1239.     begin
  1240.     print ('File=');
  1241.     print_pool_str (area); print (name_of_file,',');
  1242.     file_nm_size_overflow;
  1243.     end;
  1244. name_ptr := name_length;
  1245. while (name_ptr > 0) do        {shift up name}
  1246.     begin
  1247.     name_of_file[name_ptr+length(area)] := name_of_file[name_ptr];
  1248.     decr(name_ptr);
  1249.     end;
  1250. name_ptr := 1;
  1251. p_ptr := str_start[area];
  1252. while (p_ptr < str_start[area+1]) do
  1253.     begin
  1254.     name_of_file[name_ptr] := chr (str_pool[p_ptr]);
  1255.     incr(name_ptr); incr(p_ptr);
  1256.     end;
  1257. name_length := name_length + length(area);
  1258. This system-independent procedure converts upper-case characters to
  1259. lower case for the specified part of |buf|.  It is system independent
  1260. because it uses only the internal representation for characters.
  1261. @d case_difference = "a" - "A"
  1262. @<Procedures and functions for handling numbers, characters, and strings@>=
  1263. procedure lower_case (var buf:buf_type; @!bf_ptr,@!len:buf_pointer);
  1264. var i:buf_pointer;
  1265. begin
  1266. if (len > 0) then
  1267.   for i := bf_ptr to bf_ptr+len-1 do
  1268.     if ((buf[i]>="A") and (buf[i]<="Z")) then
  1269.     buf[i] := buf[i] + case_difference;
  1270. This system-independent procedure is the same as the previous except
  1271. that it converts lower- to upper-case letters.
  1272. @<Procedures and functions for handling numbers, characters, and strings@>=
  1273. procedure upper_case (var buf:buf_type; @!bf_ptr,@!len:buf_pointer);
  1274. var i:buf_pointer;
  1275. begin
  1276. if (len > 0) then
  1277.   for i := bf_ptr to bf_ptr+len-1 do
  1278.     if ((buf[i]>="a") and (buf[i]<="z")) then
  1279.     buf[i] := buf[i] - case_difference;
  1280. @* The hash table.
  1281. All static strings that \BibTeX\ might have to search for, generally
  1282. identifiers, are stored and retrieved by means of a fairly standard
  1283. hash-table algorithm (but slightly altered here) called the method of
  1284. ``coalescing lists''
  1285. (cf.\ Algorithm 6.4C in {\sl The Art of Computer Programming}).
  1286. Once a string enters the table, it is never removed.  The actual
  1287. sequence of characters forming a string is stored in the |str_pool|
  1288. array.
  1289. The hash table consists of the four arrays |hash_next|, |hash_text|,
  1290. |hash_ilk|, and |ilk_info|.  The first array, |hash_next[p]|, points
  1291. to the next identifier belonging to the same coalesced list as the
  1292. identifier corresponding to~|p|.  The second, |hash_text[p]|, points
  1293. to the |str_start| entry for |p|'s string. If position~|p| of the hash
  1294. table is empty, we have |hash_text[p]=0|; if position |p| is either
  1295. empty or the end of a coalesced hash list, we have
  1296. |hash_next[p]=empty|; an auxiliary pointer variable called |hash_used|
  1297. is maintained in such a way that all locations |p>=hash_used| are
  1298. nonempty.  The third, |hash_ilk[p]|, tells how this string is used (as
  1299. ordinary text, as a variable name, as an \.{.aux} file command, etc).
  1300. The fourth, |ilk_info[p]|, contains information specific to the
  1301. corresponding |hash_ilk|---for |integer_ilk|s: the integer's value;
  1302. for |cite_ilk|s: a pointer into |cite_list|; for |lc_cite_ilk|s: a
  1303. pointer to a |cite_ilk| string; for |command_ilk|s: a constant to be
  1304. used in a |case| statement; for |bst_fn_ilk|s: function-specific
  1305. information; for |macro_ilk|s: a pointer to its definition string; for
  1306. |control_seq_ilk|s: a constant for use in a |case| statement; for all
  1307. other |ilk|s it contains no information.  This |ilk|-specific
  1308. information is set in other parts of the program rather than here in
  1309. the hashing routine.
  1310. @d hash_base = empty + 1        {lowest numbered hash-table location}
  1311. @d hash_max = hash_base + hash_size - 1 {highest numbered hash-table location}
  1312. @d hash_is_full == (hash_used=hash_base) {test if all positions are occupied}
  1313. @d text_ilk = 0        {a string of ordinary text}
  1314. @d integer_ilk = 1    {an integer (possibly with a |minus_sign|)}
  1315. @d aux_command_ilk = 2    {an \.{.aux}-file command}
  1316. @d aux_file_ilk = 3    {an \.{.aux} file name}
  1317. @d bst_command_ilk = 4    {a \.{.bst}-file command}
  1318. @d bst_file_ilk = 5    {a \.{.bst} file name}
  1319. @d bib_file_ilk = 6    {a \.{.bib} file name}
  1320. @d file_ext_ilk = 7    {one of \.{.aux}, \.{.bst}, \.{.bib}, \.{.bbl},
  1321.                                 or \.{.blg}}
  1322. @d file_area_ilk = 8    {one of \.{texinputs:} or \.{texbib:}}
  1323. @d cite_ilk = 9        {a \.{\\citation} argument}
  1324. @d lc_cite_ilk = 10    {a \.{\\citation} argument converted to lower case}
  1325. @d bst_fn_ilk = 11    {a \.{.bst} function name}
  1326. @d bib_command_ilk = 12    {a \.{.bib}-file command}
  1327. @d macro_ilk = 13    {a \.{.bst} macro or a \.{.bib} string}
  1328. @d control_seq_ilk = 14    {a control sequence specifying a foreign character}
  1329. @d last_ilk = 14    {the same number as on the line above}
  1330. @<Types in the outer block@>=
  1331. @!hash_loc=hash_base..hash_max;        {a location within the hash table}
  1332. @!hash_pointer=empty..hash_max;        {either |empty| or a |hash_loc|}
  1333. @!str_ilk=0..last_ilk;    {the legal string types}
  1334. @<Globals in the outer block@>=
  1335. @!hash_next : packed array[hash_loc] of hash_pointer;    {coalesced-list link}
  1336. @!hash_text : packed array[hash_loc] of str_number;    {pointer to a string}
  1337. @!hash_ilk : packed array[hash_loc] of str_ilk;        {the type of string}
  1338. @!ilk_info : packed array[hash_loc] of integer;        {|ilk|-specific info}
  1339. @!hash_used : hash_base..hash_max+1;    {allocation pointer for hash table}
  1340. @!hash_found : boolean;     {set to |true| if it's already in the hash table}
  1341. @!dummy_loc : hash_loc;     {receives |str_lookup| value whenever it's useless}
  1342. @<Local variables for initialization@>=
  1343. @!k:hash_loc;
  1344. Now it's time to initialize the hash table; note that |str_start[0]|
  1345. must be unused if |hash_text[k] := 0| is to have the desired effect.
  1346. @<Set initial values of key variables@>=
  1347. for k:=hash_base to hash_max do
  1348.     begin
  1349.     hash_next[k] := empty;
  1350.     hash_text[k] := 0;    {thus, no need to initialize |hash_ilk| or |ilk_info|}
  1351.     end;
  1352. hash_used := hash_max + 1;    {nothing in table initially}
  1353. Here is the subroutine that searches the hash table for a
  1354. (string,~|str_ilk|) pair, where the string is of length |l>=0| and
  1355. appears in |buffer[j..(j+l-1)]|.  If it finds the pair, it returns the
  1356. corresponding hash-table location and sets the global variable
  1357. |hash_found| to |true|.  Otherwise it sets |hash_found| to |false|,
  1358. and if the parameter |insert_it| is |true|, it inserts the pair into
  1359. the hash table, inserts the string into |str_pool| if not previously
  1360. encountered, and returns its location.  Note that two different pairs
  1361. can have the same string but different |str_ilk|s, in which case the
  1362. second pair encountered, if |insert_it| were |true|, would be inserted
  1363. into the hash table though its string wouldn't be inserted into
  1364. |str_pool| because it would already be there.
  1365. @d max_hash_value = hash_prime+hash_prime-2+127        {|h|'s maximum value}
  1366. @d do_insert == true        {insert string if not found in hash table}
  1367. @d dont_insert == false        {don't insert string}
  1368. @d str_found = 40        {go here when you've found the string}
  1369. @d str_not_found = 45        {go here when you haven't}
  1370. @<Procedures and functions for handling numbers, characters, and strings@>=
  1371. function str_lookup(var buf:buf_type; @!j,@!l:buf_pointer; @!ilk:str_ilk;
  1372.         @!insert_it:boolean) : hash_loc;    {search the hash table}
  1373. label str_found,@!str_not_found;
  1374. var h:0..max_hash_value;    {hash code}
  1375. @!p:hash_loc;        {index into |hash_| arrays}
  1376. @!k:buf_pointer;    {index into |buf| array}
  1377. @!old_string:boolean;    {set to |true| if it's an already encountered string}
  1378. @!str_num:str_number;    {pointer to an already encountered string}
  1379. begin
  1380. @<Compute the hash code |h|@>;
  1381. p:=h+hash_base;        {start searching here; note that |0<=h<hash_prime|}
  1382. hash_found := false;
  1383. old_string := false;
  1384.     begin
  1385.     @<Process the string if we've already encountered it@>;
  1386.     if (hash_next[p]=empty) then    {location |p| may or may not be empty}
  1387.     begin
  1388.     if (not insert_it) then goto str_not_found;
  1389.     @<Insert pair into hash table and make |p| point to it@>;
  1390.     goto str_found;
  1391.     end;
  1392.     p:=hash_next[p];        {old and new locations |p| are not empty}
  1393.     end;
  1394. str_not_found: do_nothing;    {don't insert pair; function value meaningless}
  1395. str_found: str_lookup:=p;
  1396. @^for loops@>
  1397. @.WEB@>
  1398. The value of |hash_prime| should be roughly 85\% of |hash_size|, and
  1399. it should be a prime number
  1400. (it should also be less than $2^{14} + 2^{6} = 16320$ because of
  1401. \.{WEB}'s simple-macro bound).  The theory of hashing tells us to expect
  1402. fewer than two table probes, on the average, when the search is
  1403. successful.
  1404. @<Compute the hash code |h|@>=
  1405. begin
  1406. h := 0;        {note that this works for zero-length strings}
  1407. k := j;
  1408. while (k < j+l) do    {not a |for| loop in case |j = l = 0|}
  1409.     begin
  1410.     h:=h+h+buf[k];
  1411.     while (h >= hash_prime) do h:=h-hash_prime;
  1412.     incr(k);
  1413.     end;
  1414. Here we handle the case in which we've already encountered this
  1415. string; note that even if we have, we'll still have to insert the pair
  1416. into the hash table if |str_ilk| doesn't match.
  1417. @<Process the string if we've already encountered it@>=
  1418. begin
  1419. if (hash_text[p]>0) then            {there's something here}
  1420.     if (str_eq_buf(hash_text[p],buf,j,l)) then    {it's the right string}
  1421.     if (hash_ilk[p] = ilk) then        {it's the right |str_ilk|}
  1422.         begin
  1423.         hash_found := true;
  1424.         goto str_found;
  1425.         end
  1426.       else
  1427.         begin                {it's the wrong |str_ilk|}
  1428.         old_string := true;
  1429.         str_num := hash_text[p];
  1430.         end;
  1431. @^for loops@>
  1432. @:BibTeX capacity exceeded}{\quad hash size@>
  1433. This code inserts the pair in the appropriate unused location.
  1434. @<Insert pair into hash table and make |p| point to it@>=
  1435. begin
  1436. if (hash_text[p]>0) then        {location |p| isn't empty}
  1437.     begin
  1438.     repeat if (hash_is_full) then overflow('hash size ',hash_size);
  1439.     decr(hash_used);
  1440.     until (hash_text[hash_used]=0);    {search for an empty location}
  1441.     hash_next[p]:=hash_used;
  1442.     p:=hash_used;
  1443.     end;            {now location |p| is empty}
  1444. if (old_string) then        {it's an already encountered string}
  1445.     hash_text[p] := str_num
  1446.   else
  1447.     begin            {it's a new string}
  1448.     str_room(l);        {make sure it'll fit in |str_pool|}
  1449.     k := j;
  1450.     while (k < j+l) do        {not a |for| loop in case |j = l = 0|}
  1451.     begin
  1452.     append_char(buf[k]);
  1453.     incr(k);
  1454.     end;
  1455.     hash_text[p] := make_string;        {and make it official}
  1456.     end;
  1457. hash_ilk[p] := ilk;
  1458. @^string pool@>
  1459. Now that we've defined the hash-table workings we can initialize the
  1460. string pool.  Unlike \TeX, \BibTeX\ does not use a |pool_file| for
  1461. string storage; instead it inserts its pre-defined strings into
  1462. |str_pool|---this makes one file fewer for the \BibTeX\ implementor
  1463. to deal with.  This section initializes |str_pool|; the pre-defined
  1464. strings will be inserted into it shortly; and other strings are
  1465. inserted while processing the input files.
  1466. @<Set initial values of key variables@>=
  1467. pool_ptr:=0; str_ptr:=1;    {hash table must have |str_start[0]| unused}
  1468. str_start[str_ptr]:=pool_ptr;
  1469. The longest pre-defined string determines type definitions used to
  1470. insert the pre-defined strings into |str_pool|.
  1471. @d longest_pds=12    {the length of `\.{change.case\$}'}
  1472. @<Types in the outer block@>=
  1473. @!pds_loc = 1..longest_pds;
  1474. @!pds_len = 0..longest_pds;
  1475. @!pds_type = packed array [pds_loc] of char;
  1476. The variables in this program beginning with |s_| specify the
  1477. locations in |str_pool| for certain often-used strings.  Those here
  1478. have to do with the file system; the next section will actually insert
  1479. them into |str_pool|.
  1480. @<Globals in the outer block@>=
  1481. @!s_aux_extension : str_number;    {\.{.aux}}
  1482. @!s_log_extension : str_number;    {\.{.blg}}
  1483. @!s_bbl_extension : str_number;    {\.{.bbl}}
  1484. @!s_bst_extension : str_number;    {\.{.bst}}
  1485. @!s_bib_extension : str_number;    {\.{.bib}}
  1486. @!s_bst_area : str_number;    {\.{texinputs:}}
  1487. @!s_bib_area : str_number;    {\.{texbib:}}
  1488. @^important note@>
  1489. @^system dependencies@>
  1490. It's time to insert some of the pre-defined strings into |str_pool|
  1491. (and thus the hash table).  These system-dependent strings should
  1492. contain no upper-case letters, and they must all be exactly
  1493. |longest_pds| characters long (even if fewer characters are actually
  1494. stored).  The |pre_define| routine appears shortly.
  1495. Important notes: These pre-definitions must not have any glitches or
  1496. the program may bomb because the |log_file| hasn't been opened yet,
  1497. and |text_ilk|s should be pre-defined later, for
  1498. \.{.bst}-function-execution purposes.
  1499. @<Pre-define certain strings@>=
  1500. pre_define('.aux        ',4,file_ext_ilk);
  1501. s_aux_extension := hash_text[pre_def_loc];
  1502. pre_define('.bbl        ',4,file_ext_ilk);
  1503. s_bbl_extension := hash_text[pre_def_loc];
  1504. pre_define('.blg        ',4,file_ext_ilk);
  1505. s_log_extension := hash_text[pre_def_loc];
  1506. pre_define('.bst        ',4,file_ext_ilk);
  1507. s_bst_extension := hash_text[pre_def_loc];
  1508. pre_define('.bib        ',4,file_ext_ilk);
  1509. s_bib_extension := hash_text[pre_def_loc];
  1510. pre_define('texinputs:  ',10,file_area_ilk);
  1511. s_bst_area := hash_text[pre_def_loc];
  1512. pre_define('texbib:     ',7,file_area_ilk);
  1513. s_bib_area := hash_text[pre_def_loc];
  1514. This global variable gives the hash-table location of pre-defined
  1515. strings generated by calls to |str_lookup|.
  1516. @<Globals in the outer block@>=
  1517. @!pre_def_loc : hash_loc;
  1518. This procedure initializes a pre-defined string of length at most
  1519. |longest_pds|.
  1520. @<Procedures and functions for handling numbers, characters, and strings@>=
  1521. procedure pre_define (@!pds:pds_type; @!len:pds_len; @!ilk:str_ilk);
  1522. var i : pds_len;
  1523. begin
  1524. for i:=1 to len do
  1525.     buffer[i] := xord[pds[i]];
  1526. pre_def_loc := str_lookup(buffer,1,len,ilk,do_insert);
  1527. These constants all begin with |n_| and are used for the |case|
  1528. statement that determines which command to execute.  The variable
  1529. |command_num| is set to one of these and is used to do the branching,
  1530. but it must have the full |integer| range because at times it can
  1531. assume an arbitrary |ilk_info| value (though it will be one of the
  1532. values here when we actually use it).
  1533. @d n_aux_bibdata = 0    {\.{\\bibdata}}
  1534. @d n_aux_bibstyle = 1    {\.{\\bibstyle}}
  1535. @d n_aux_citation = 2    {\.{\\citation}}
  1536. @d n_aux_input = 3    {\.{\\@@input}}
  1537. @d n_bst_entry = 0    {\.{entry}}
  1538. @d n_bst_execute = 1    {\.{execute}}
  1539. @d n_bst_function = 2    {\.{function}}
  1540. @d n_bst_integers = 3    {\.{integers}}
  1541. @d n_bst_iterate = 4    {\.{iterate}}
  1542. @d n_bst_macro = 5    {\.{macro}}
  1543. @d n_bst_read = 6    {\.{read}}
  1544. @d n_bst_reverse = 7    {\.{reverse}}
  1545. @d n_bst_sort = 8    {\.{sort}}
  1546. @d n_bst_strings = 9    {\.{strings}}
  1547. @d n_bib_comment = 0    {\.{comment}}
  1548. @d n_bib_preamble = 1    {\.{preamble}}
  1549. @d n_bib_string = 2    {\.{string}}
  1550. @<Globals in the outer block@>=
  1551. @!command_num : integer;
  1552. @^important note@>
  1553. Now we pre-define the command strings; they must all be exactly
  1554. |longest_pds| characters long.
  1555. Important note: These pre-definitions must not have any glitches or
  1556. the program may bomb because the |log_file| hasn't been opened yet.
  1557. @<Pre-define certain strings@>=
  1558. pre_define('\citation   ',9,aux_command_ilk);
  1559. ilk_info[pre_def_loc] := n_aux_citation;
  1560. pre_define('\bibdata    ',8,aux_command_ilk);
  1561. ilk_info[pre_def_loc] := n_aux_bibdata;
  1562. pre_define('\bibstyle   ',9,aux_command_ilk);
  1563. ilk_info[pre_def_loc] := n_aux_bibstyle;
  1564. pre_define('\@@input     ',7,aux_command_ilk);
  1565. ilk_info[pre_def_loc] := n_aux_input;
  1566. pre_define('entry       ',5,bst_command_ilk);
  1567. ilk_info[pre_def_loc] := n_bst_entry;
  1568. pre_define('execute     ',7,bst_command_ilk);
  1569. ilk_info[pre_def_loc] := n_bst_execute;
  1570. pre_define('function    ',8,bst_command_ilk);
  1571. ilk_info[pre_def_loc] := n_bst_function;
  1572. pre_define('integers    ',8,bst_command_ilk);
  1573. ilk_info[pre_def_loc] := n_bst_integers;
  1574. pre_define('iterate     ',7,bst_command_ilk);
  1575. ilk_info[pre_def_loc] := n_bst_iterate;
  1576. pre_define('macro       ',5,bst_command_ilk);
  1577. ilk_info[pre_def_loc] := n_bst_macro;
  1578. pre_define('read        ',4,bst_command_ilk);
  1579. ilk_info[pre_def_loc] := n_bst_read;
  1580. pre_define('reverse     ',7,bst_command_ilk);
  1581. ilk_info[pre_def_loc] := n_bst_reverse;
  1582. pre_define('sort        ',4,bst_command_ilk);
  1583. ilk_info[pre_def_loc] := n_bst_sort;
  1584. pre_define('strings     ',7,bst_command_ilk);
  1585. ilk_info[pre_def_loc] := n_bst_strings;
  1586. pre_define('comment     ',7,bib_command_ilk);
  1587. ilk_info[pre_def_loc] := n_bib_comment;
  1588. pre_define('preamble    ',8,bib_command_ilk);
  1589. ilk_info[pre_def_loc] := n_bib_preamble;
  1590. pre_define('string      ',6,bib_command_ilk);
  1591. ilk_info[pre_def_loc] := n_bib_string;
  1592. @* Scanning an input line.
  1593. This section describes the various |buffer| scanning routines.  The
  1594. two global variables |buf_ptr1| and |buf_ptr2| are used in scanning an
  1595. input line.  Between scans, |buf_ptr1| points to the first character
  1596. of the current token and |buf_ptr2| points to that of the next.  The
  1597. global variable |last|, set by the function |input_ln|, marks the end
  1598. of the current line; it equals 0 at the end of the current file.  All
  1599. the procedures and functions in this section will indicate an
  1600. end-of-line when it's the end of the file.
  1601. @d token_len == (buf_ptr2 - buf_ptr1)    {of the current token}
  1602. @d scan_char == buffer[buf_ptr2]    {the current character}
  1603. @<Globals in the outer block@>=
  1604. @!buf_ptr1:buf_pointer; {points to the first position of the current token}
  1605. @!buf_ptr2:buf_pointer; {used to find the end of the current token}
  1606. These macros send the current token, in |buffer[buf_ptr1]| to
  1607. |buffer[buf_ptr2-1]|, to an output file.
  1608. @d print_token == print_a_token    {making this a procedure saves a little space}
  1609. @d trace_pr_token == begin
  1610.              out_token(log_file);
  1611.              end
  1612. @^system dependencies@>
  1613. And here are the associated procedures.  Note: The |term_out| file is
  1614. system dependent.
  1615. @<Procedures and functions for all file I/O, error messages, and such@>=
  1616. procedure out_token (var f:alpha_file);
  1617. var i:buf_pointer;
  1618. begin
  1619. i := buf_ptr1;
  1620. while (i < buf_ptr2) do
  1621.     begin
  1622.     write(f,xchr[buffer[i]]);
  1623.     incr(i);
  1624.     end;
  1625. procedure print_a_token;
  1626. begin
  1627. out_token(term_out);
  1628. out_token(log_file);
  1629. This function scans the |buffer| for the next token, starting at the
  1630. global variable |buf_ptr2| and ending just before either the single
  1631. specified stop-character or the end of the current line, whichever
  1632. comes first, respectively returning |true| or |false|; afterward,
  1633. |scan_char| is the first character following this token.
  1634. @<Procedures and functions for input scanning@>=
  1635. function scan1 (@!char1:ASCII_code) : boolean;
  1636. begin
  1637. buf_ptr1 := buf_ptr2;
  1638.             {scan until end-of-line or the specified character}
  1639. while ((scan_char <> char1) and (buf_ptr2 < last)) do
  1640.     incr(buf_ptr2);
  1641. if (buf_ptr2 < last) then
  1642.     scan1 := true
  1643.   else
  1644.     scan1 := false;
  1645. This function is the same but stops at |white_space| characters as well.
  1646. @<Procedures and functions for input scanning@>=
  1647. function scan1_white (@!char1:ASCII_code) : boolean;
  1648. begin
  1649. buf_ptr1 := buf_ptr2;
  1650.     {scan until end-of-line, the specified character, or |white_space|}
  1651. while ((lex_class[scan_char] <> white_space) and (scan_char <> char1) and
  1652.                             (buf_ptr2 < last)) do
  1653.     incr(buf_ptr2);
  1654. if (buf_ptr2 < last) then
  1655.     scan1_white := true
  1656.   else
  1657.     scan1_white := false;
  1658. This function is similar to |scan1|, but stops at either of two
  1659. stop-characters as well as the end of the current line.
  1660. @<Procedures and functions for input scanning@>=
  1661. function scan2 (@!char1,@!char2:ASCII_code) : boolean;
  1662. begin
  1663. buf_ptr1 := buf_ptr2;
  1664.             {scan until end-of-line or the specified characters}
  1665. while ((scan_char <> char1) and    (scan_char <> char2) and (buf_ptr2 < last)) do
  1666.     incr(buf_ptr2);
  1667. if (buf_ptr2 < last) then
  1668.     scan2 := true
  1669.   else
  1670.     scan2 := false;
  1671. This function is the same but stops at |white_space| characters as well.
  1672. @<Procedures and functions for input scanning@>=
  1673. function scan2_white (@!char1,@!char2:ASCII_code) : boolean;
  1674. begin
  1675. buf_ptr1 := buf_ptr2;
  1676.     {scan until end-of-line, the specified characters, or |white_space|}
  1677. while ((scan_char <> char1) and (scan_char <> char2) and
  1678.         (lex_class[scan_char] <> white_space) and (buf_ptr2 < last)) do
  1679.     incr(buf_ptr2);
  1680. if (buf_ptr2 < last) then
  1681.     scan2_white := true
  1682.   else
  1683.     scan2_white := false;
  1684. This function is similar to |scan2|, but stops at either of three
  1685. stop-characters as well as the end of the current line.
  1686. @<Procedures and functions for input scanning@>=
  1687. function scan3 (@!char1,@!char2,@!char3:ASCII_code) : boolean;
  1688. begin
  1689. buf_ptr1 := buf_ptr2;
  1690.             {scan until end-of-line or the specified characters}
  1691. while ((scan_char <> char1) and (scan_char <> char2) and
  1692.                 (scan_char <> char3) and (buf_ptr2 < last)) do
  1693.     incr(buf_ptr2);
  1694. if (buf_ptr2 < last) then
  1695.     scan3 := true
  1696.   else
  1697.     scan3 := false;
  1698. This function scans for letters, stopping at the first nonletter; it
  1699. returns |true| if there is at least one letter.
  1700. @<Procedures and functions for input scanning@>=
  1701. function scan_alpha : boolean;
  1702. begin
  1703. buf_ptr1 := buf_ptr2;
  1704.                     {scan until end-of-line or a nonletter}
  1705. while ((lex_class[scan_char] = alpha) and (buf_ptr2 < last)) do
  1706.     incr(buf_ptr2);
  1707. if (token_len = 0) then
  1708.     scan_alpha := false
  1709.   else
  1710.     scan_alpha := true;
  1711. These are the possible values for |scan_result|; they're set by the
  1712. |scan_identifier| procedure and are described in the next section.
  1713. @d id_null = 0
  1714. @d specified_char_adjacent = 1
  1715. @d other_char_adjacent = 2
  1716. @d white_adjacent = 3
  1717. @<Globals in the outer block@>=
  1718. @!scan_result : id_null..white_adjacent;
  1719. This procedure scans for an identifier, stopping at the first
  1720. |illegal_id_char|, or stopping at the first character if it's
  1721. |numeric|.  It sets the global variable |scan_result| to |id_null| if
  1722. the identifier is null, else to |white_adjacent| if it ended at a
  1723. |white_space| character or an end-of-line, else to
  1724. |specified_char_adjacent| if it ended at one of |char1| or |char2| or
  1725. |char3|, else to |other_char_adjacent| if it ended at a nonspecified,
  1726. non|white_space| |illegal_id_char|.  By convention, when some calling
  1727. code really wants just one or two ``specified'' characters, it merely
  1728. repeats one of the characters.
  1729. @<Procedures and functions for input scanning@>=
  1730. procedure scan_identifier (@!char1,@!char2,@!char3:ASCII_code);
  1731. begin
  1732. buf_ptr1 := buf_ptr2;
  1733. if (lex_class[scan_char] <> numeric) then
  1734.             {scan until end-of-line or an |illegal_id_char|}
  1735.     while ((id_class[scan_char] = legal_id_char) and (buf_ptr2 < last)) do
  1736.     incr(buf_ptr2);
  1737. if (token_len = 0) then
  1738.     scan_result := id_null
  1739. else if ((lex_class[scan_char] = white_space) or (buf_ptr2 = last)) then
  1740.     scan_result := white_adjacent
  1741. else if ((scan_char = char1) or (scan_char = char2) or (scan_char = char3))
  1742.                                     then
  1743.     scan_result := specified_char_adjacent
  1744.     scan_result := other_char_adjacent;
  1745. The next two procedures scan for an integer, setting the global
  1746. variable |token_value| to the corresponding integer.
  1747. @d char_value == (scan_char - "0")    {the value of the digit being scanned}
  1748. @<Globals in the outer block@>=
  1749. @!token_value : integer;    {the numeric value of the current token}
  1750. This function scans for a nonnegative integer, stopping at the first
  1751. nondigit; it sets the value of |token_value| accordingly.  It returns
  1752. |true| if the token was a legal nonnegative integer (i.e., consisted
  1753. of one or more digits).
  1754. @<Procedures and functions for input scanning@>=
  1755. function scan_nonneg_integer : boolean;
  1756. begin
  1757. buf_ptr1 := buf_ptr2;
  1758. token_value := 0;
  1759.                     {scan until end-of-line or a nondigit}
  1760. while ((lex_class[scan_char] = numeric) and (buf_ptr2 < last)) do
  1761.     begin
  1762.     token_value := token_value*10 + char_value;
  1763.     incr(buf_ptr2);
  1764.     end;
  1765. if (token_len = 0) then            {there were no digits}
  1766.     scan_nonneg_integer := false
  1767.   else
  1768.     scan_nonneg_integer := true;
  1769. This procedure scans for an integer, stopping at the first nondigit;
  1770. it sets the value of |token_value| accordingly.  It returns |true| if
  1771. the token was a legal integer (i.e., consisted of an optional
  1772. |minus_sign| followed by one or more digits).
  1773. @d negative == (sign_length = 1)    {if this integer is negative}
  1774. @<Procedures and functions for input scanning@>=
  1775. function scan_integer : boolean;
  1776. var sign_length : 0..1;        {1 if there's a |minus_sign|, 0 if not}
  1777. begin
  1778. buf_ptr1 := buf_ptr2;
  1779. if (scan_char = minus_sign) then    {it's a negative number}
  1780.     begin
  1781.     sign_length := 1;
  1782.     incr(buf_ptr2);            {skip over the |minus_sign|}
  1783.     end
  1784.   else
  1785.     sign_length := 0;
  1786. token_value := 0;
  1787.                     {scan until end-of-line or a nondigit}
  1788. while ((lex_class[scan_char] = numeric) and (buf_ptr2 < last)) do
  1789.     begin
  1790.     token_value := token_value*10 + char_value;
  1791.     incr(buf_ptr2);
  1792.     end;
  1793. if (negative) then
  1794.     token_value := -token_value;
  1795. if (token_len = sign_length) then    {there were no digits}
  1796.     scan_integer := false
  1797.   else
  1798.     scan_integer := true;
  1799. This function scans over |white_space| characters, stopping either at
  1800. the first nonwhite character or the end of the line, respectively
  1801. returning |true| or |false|.
  1802. @<Procedures and functions for input scanning@>=
  1803. function scan_white_space : boolean;
  1804. begin
  1805.                     {scan until end-of-line or a nonwhite}
  1806. while ((lex_class[scan_char] = white_space) and (buf_ptr2 < last)) do
  1807.     incr(buf_ptr2);
  1808. if (buf_ptr2 < last) then
  1809.     scan_white_space := true
  1810.   else
  1811.     scan_white_space := false;
  1812. The |print_bad_input_line| procedure prints the current input line,
  1813. splitting it at the character being scanned: It prints |buffer[0]|,
  1814. |buffer[1]|, \dots, |buffer[buf_ptr2-1]| on one line and
  1815. |buffer[buf_ptr2]|, \dots, |buffer[last-1]| on the next (and both
  1816. lines start with a colon between two |space|s).  Each |white_space|
  1817. character is printed as a |space|.
  1818. @<Procedures and functions for all file I/O, error messages, and such@>=
  1819. procedure print_bad_input_line;
  1820. var bf_ptr : buf_pointer;
  1821. begin
  1822. print (' : ');
  1823. bf_ptr := 0;
  1824. while (bf_ptr < buf_ptr2) do
  1825.     begin
  1826.     if (lex_class[buffer[bf_ptr]] = white_space) then
  1827.     print (xchr[space])
  1828.       else
  1829.     print (xchr[buffer[bf_ptr]]);
  1830.     incr(bf_ptr);
  1831.     end;
  1832. print_newline;
  1833. print (' : ');
  1834. bf_ptr := 0;
  1835. while (bf_ptr < buf_ptr2) do
  1836.     begin
  1837.     print (xchr[space]);
  1838.     incr(bf_ptr);
  1839.     end;
  1840. bf_ptr := buf_ptr2;
  1841. while (bf_ptr < last) do
  1842.     begin
  1843.     if (lex_class[buffer[bf_ptr]] = white_space) then
  1844.     print (xchr[space])
  1845.       else
  1846.     print (xchr[buffer[bf_ptr]]);
  1847.     incr(bf_ptr);
  1848.     end;
  1849. print_newline;@/
  1850. bf_ptr := 0;
  1851. while ((bf_ptr < buf_ptr2) and (lex_class[buffer[bf_ptr]] = white_space)) do
  1852.     incr(bf_ptr);
  1853. if (bf_ptr = buf_ptr2) then
  1854.     print_ln ('(Error may have been on previous line)');
  1855. mark_error;
  1856. This little procedure exists because it's used by at least two other
  1857. procedures and thus saves some space.
  1858. @<Procedures and functions for all file I/O, error messages, and such@>=
  1859. procedure print_skipping_whatever_remains;
  1860. begin
  1861. print ('I''m skipping whatever remains of this ');
  1862. @* Getting the top-level auxiliary file name.
  1863. @^system dependencies@>
  1864. These modules read the name of the top-level \.{.aux} file.  Some
  1865. systems will try to find this on the command line; if it's not there
  1866. it will come from the user's terminal.  In either case, the name goes
  1867. into the |char| array |name_of_file|, and the files relevant to this
  1868. name are opened.
  1869. @d aux_found=41        {go here when the \.{.aux} name is legit}
  1870. @d aux_not_found=46    {go here when it's not}
  1871. @<Globals in the outer block@>=
  1872. @!aux_name_length : 0..file_name_size+1;    {\.{.aux} name sans extension}
  1873. @^system dependencies@>
  1874. @^user abuse@>
  1875. I mean, this is truly disgraceful.  A user has to type something in to
  1876. the terminal just once during the entire run.  And it's not some
  1877. complicated string where you have to get every last punctuation mark
  1878. just right, and it's not some fancy list where you get nervous because
  1879. if you forget one item you have to type the whole thing again; it's
  1880. just a simple, ordinary, file name.  Now you'd think a five-year-old
  1881. could do it; you'd think it's so simple a user should be able to do it
  1882. in his sleep.  But noooooooooo.  He had to sit there droning on and on
  1883. about who knows what until he exceeded the bounds of common sense, and
  1884. he probably didn't even realize it.  Just pitiful.  What's this world
  1885. coming to?  We should probably just delete all his files and be done
  1886. with him.  Note: The |term_out| file is system dependent.
  1887. @d sam_you_made_the_file_name_too_long == begin
  1888.                       sam_too_long_file_name_print;
  1889.                       goto aux_not_found;
  1890.                       end
  1891. @<Procedures and functions for all file I/O, error messages, and such@>=
  1892. procedure sam_too_long_file_name_print;
  1893. begin
  1894. write (term_out,'File name `');
  1895. name_ptr := 1;
  1896. while (name_ptr <= aux_name_length) do
  1897.     begin
  1898.     write (term_out,name_of_file[name_ptr]);
  1899.     incr(name_ptr);
  1900.     end;
  1901. write_ln (term_out,''' is too long');
  1902. @^system dependencies@>
  1903. @^user abuse@>
  1904. We've abused the user enough for one section; suffice it to
  1905. say here that most of what we said last module still applies.
  1906. Note: The |term_out| file is system dependent.
  1907. @d sam_you_made_the_file_name_wrong == begin
  1908.                        sam_wrong_file_name_print;
  1909.                        goto aux_not_found;
  1910.                        end
  1911. @<Procedures and functions for all file I/O, error messages, and such@>=
  1912. procedure sam_wrong_file_name_print;
  1913. begin
  1914. write (term_out,'I couldn''t open file name `');
  1915. name_ptr := 1;
  1916. while (name_ptr <= name_length) do
  1917.     begin
  1918.     write (term_out,name_of_file[name_ptr]);
  1919.     incr(name_ptr);
  1920.     end;
  1921. write_ln (term_out,'''');
  1922. @^system dependencies@>
  1923. This procedure consists of a loop that reads and processes a (nonnull)
  1924. \.{.aux} file name.  It's this module and the next two that must be
  1925. changed on those systems using command-line arguments.  Note: The
  1926. |term_out| and |term_in| files are system dependent.
  1927. @<Procedures and functions for the reading and processing of input files@>=
  1928. procedure get_the_top_level_aux_file_name;
  1929. label aux_found,@!aux_not_found;
  1930. var @<Variables for possible command-line processing@>@/
  1931. begin
  1932. check_cmnd_line := false;            {many systems will change this}
  1933.     begin
  1934.     if (check_cmnd_line) then
  1935.     @<Process a possible command line@>
  1936.       else
  1937.     begin
  1938.     write (term_out,'Please type input file name (no extension)--');
  1939.     if (eoln(term_in)) then            {so the first |read| works}
  1940.         read_ln (term_in);
  1941.     aux_name_length := 0;
  1942.     while (not eoln(term_in)) do
  1943.         begin
  1944.         if (aux_name_length = file_name_size) then
  1945.         begin
  1946.         while (not eoln(term_in)) do    {discard the rest of the line}
  1947.             get(term_in);
  1948.         sam_you_made_the_file_name_too_long;
  1949.         end;
  1950.         incr(aux_name_length);
  1951.         name_of_file[aux_name_length] := term_in^;
  1952.         get(term_in);
  1953.         end;
  1954.     end;
  1955.     @<Handle this \.{.aux} name@>;
  1956. aux_not_found:
  1957.     check_cmnd_line := false;
  1958.     end;
  1959. aux_found:            {now we're ready to read the \.{.aux} file}
  1960. @^system dependencies@>
  1961. The switch |check_cmnd_line| tells us whether we're to check for a
  1962. possible command-line argument.
  1963. @<Variables for possible command-line processing@>=
  1964. @!check_cmnd_line : boolean;    {|true| if we're to check the command line}
  1965. @^system dependencies@>
  1966. Here's where we do the real command-line work.  Those systems needing
  1967. more than a single module to handle the task should add the extras to
  1968. the ``System-dependent changes'' section.
  1969. @<Process a possible command line@>=
  1970. begin
  1971. do_nothing;        {the ``default system'' doesn't use the command line}
  1972. Here we orchestrate this \.{.aux} name's handling: we add the various
  1973. extensions, try to open the files with the resulting name, and
  1974. store the name strings we'll need later.
  1975. @<Handle this \.{.aux} name@>=
  1976. begin
  1977. if ((aux_name_length + length(s_aux_extension) > file_name_size) or@|
  1978.     (aux_name_length + length(s_log_extension) > file_name_size) or@|
  1979.     (aux_name_length + length(s_bbl_extension) > file_name_size)) then
  1980.     sam_you_made_the_file_name_too_long;
  1981. @<Add extensions and open files@>;
  1982. @<Put this name into the hash table@>;
  1983. goto aux_found;
  1984. Here we set up definitions and declarations for files opened in this
  1985. section.  Each element in |aux_list| (except for
  1986. |aux_list[aux_stack_size]|, which is always unused) is a pointer to
  1987. the appropriate |str_pool| string representing the \.{.aux} file name.
  1988. The array |aux_file| contains the corresponding \PASCAL\ |file|
  1989. variables.
  1990. @d cur_aux_str == aux_list[aux_ptr]  {shorthand for the current \.{.aux} file}
  1991. @d cur_aux_file == aux_file[aux_ptr]    {shorthand for the current |aux_file|}
  1992. @d cur_aux_line == aux_ln_stack[aux_ptr] {line number of current \.{.aux} file}
  1993. @<Globals in the outer block@>=
  1994. @!aux_file : array[aux_number] of alpha_file; {open \.{.aux} |file| variables}
  1995. @!aux_list : array[aux_number] of str_number;    {the open \.{.aux} file list}
  1996. @!aux_ptr : aux_number;        {points to the currently open \.{.aux} file}
  1997. @!aux_ln_stack : array[aux_number] of integer;    {open \.{.aux} line numbers}
  1998. @!top_lev_str : str_number;    {the top-level \.{.aux} file's name}
  1999. @!log_file : alpha_file;    {the |file| variable for the \.{.blg} file}
  2000. @!bbl_file : alpha_file;    {the |file| variable for the \.{.bbl} file}
  2001. Where |aux_number| is the obvious.
  2002. @<Types in the outer block@>=
  2003. @!aux_number = 0..aux_stack_size;    {gives the |aux_list| range}
  2004. @^system dependencies@>
  2005. We must make sure the (top-level) \.{.aux}, \.{.blg}, and \.{.bbl}
  2006. files can be opened.
  2007. @<Add extensions and open files@>=
  2008. begin
  2009. name_length := aux_name_length;        {set to last used position}
  2010. add_extension (s_aux_extension);     {this also sets |name_length|}
  2011. aux_ptr := 0;                {initialize the \.{.aux} file stack}
  2012. if (not a_open_in(cur_aux_file)) then
  2013.     sam_you_made_the_file_name_wrong;
  2014. name_length := aux_name_length;
  2015. add_extension (s_log_extension);     {this also sets |name_length|}
  2016. if (not a_open_out(log_file)) then
  2017.     sam_you_made_the_file_name_wrong;
  2018. name_length := aux_name_length;
  2019. add_extension (s_bbl_extension);     {this also sets |name_length|}
  2020. if (not a_open_out(bbl_file)) then
  2021.     sam_you_made_the_file_name_wrong;
  2022. @:this can't happen}{\quad Already encountered auxiliary file@>
  2023. This code puts the \.{.aux} file name, both with and without the
  2024. extension, into the hash table, and it initializes |aux_list|.  Note
  2025. that all previous top-level \.{.aux}-file stuff must have been
  2026. successful.
  2027. @<Put this name into the hash table@>=
  2028. begin
  2029. name_length := aux_name_length;
  2030. add_extension (s_aux_extension);     {this also sets |name_length|}
  2031. name_ptr := 1;
  2032. while (name_ptr <= name_length) do
  2033.     begin
  2034.     buffer[name_ptr] := xord[name_of_file[name_ptr]];
  2035.     incr(name_ptr);
  2036.     end;
  2037. top_lev_str := hash_text[
  2038.         str_lookup(buffer,1,aux_name_length,text_ilk,do_insert)];
  2039. cur_aux_str := hash_text[
  2040.         str_lookup(buffer,1,name_length,aux_file_ilk,do_insert)];
  2041.                 {note that this has initialized |aux_list|}
  2042. if (hash_found) then
  2043.     begin
  2044.       trace
  2045.       print_aux_name;
  2046.       ecart@/
  2047.     confusion ('Already encountered auxiliary file');
  2048.     end;
  2049. cur_aux_line := 0;   {this finishes initializing the top-level \.{.aux} file}
  2050. Print the name of the current \.{.aux} file, followed by a |newline|.
  2051. @<Procedures and functions for all file I/O, error messages, and such@>=
  2052. procedure print_aux_name;
  2053. begin
  2054. print_pool_str (cur_aux_str);
  2055. print_newline;
  2056. @* Reading the auxiliary file(s).
  2057. @^auxiliary-file commands@>
  2058. Now it's time to read the \.{.aux} file.  The only commands we handle
  2059. are \.{\\citation} (there can be arbitrarily many, each having
  2060. arbitrarily many arguments), \.{\\bibdata} (there can be just one, but
  2061. it can have arbitrarily many arguments), \.{\\bibstyle} (there can be
  2062. just one, and it can have just one argument), and \.{\\@@input} (there
  2063. can be arbitrarily many, each with one argument, and they can be
  2064. nested to a depth of |aux_stack_size|).  Each of these commands is
  2065. assumed to be on just a single line.  The rest of the \.{.aux} file is
  2066. ignored.
  2067. @d aux_done=31        {go here when finished with the \.{.aux} files}
  2068. @<Labels in the outer block@>=
  2069. ,@!aux_done
  2070. We keep reading and processing input lines until none left.  This is
  2071. part of the main program; hence, because of the |aux_done| label,
  2072. there's no conventional |begin|-|end| pair surrounding the entire
  2073. module.
  2074. @<Read the \.{.aux} file@>=
  2075. print ('The top-level auxiliary file: ');
  2076. print_aux_name;
  2077.     begin            {|pop_the_aux_stack| will exit the loop}
  2078.     incr(cur_aux_line);
  2079.     if (not input_ln(cur_aux_file)) then    {end of current \.{.aux} file}
  2080.     pop_the_aux_stack
  2081.       else
  2082.     get_aux_command_and_process;
  2083.     end;
  2084.   trace
  2085.   trace_pr_ln ('Finished reading the auxiliary file(s)');
  2086.   ecart@/
  2087. aux_done:
  2088. last_check_for_aux_errors;
  2089. When we find a bug, we print a message and flush the rest of the line.
  2090. This macro must be called from within a procedure that has an |exit|
  2091. label.
  2092. @d aux_err_return == begin
  2093.              aux_err_print;
  2094.              return;        {flush this input line}
  2095.              end
  2096. @d aux_err(#) == begin
  2097.          print (#);
  2098.          aux_err_return;
  2099.          end
  2100. @<Procedures and functions for all file I/O, error messages, and such@>=
  2101. procedure aux_err_print;
  2102. begin
  2103. print ('---line ',cur_aux_line:0,' of file ');
  2104. print_aux_name;@/
  2105. print_bad_input_line;            {this call does the |mark_error|}
  2106. print_skipping_whatever_remains;
  2107. print_ln ('command')
  2108. @:this can't happen}{\quad Illegal auxiliary-file command@>
  2109. Here are a bunch of macros whose print statements are used at least
  2110. twice.  Thus we save space by making the statements procedures.  This
  2111. macro complains when there's a repeated command that's to be used just
  2112. once.
  2113. @d aux_err_illegal_another(#) == begin
  2114.                  aux_err_illegal_another_print (#);
  2115.                  aux_err_return;
  2116.                  end
  2117. @<Procedures and functions for all file I/O, error messages, and such@>=
  2118. procedure aux_err_illegal_another_print (@!cmd_num : integer);
  2119. begin
  2120. print ('Illegal, another \bib');
  2121. case (cmd_num) of
  2122.     n_aux_bibdata : print ('data');
  2123.     n_aux_bibstyle : print ('style');
  2124.     othercases
  2125.     confusion ('Illegal auxiliary-file command')
  2126. endcases;
  2127. print (' command');
  2128. This one complains when a command is missing its |right_brace|.
  2129. @d aux_err_no_right_brace == begin
  2130.                  aux_err_no_right_brace_print;
  2131.                  aux_err_return;
  2132.                  end
  2133. @<Procedures and functions for all file I/O, error messages, and such@>=
  2134. procedure aux_err_no_right_brace_print;
  2135. begin
  2136. print ('No "',xchr[right_brace],'"');
  2137. This one complains when a command has stuff after its |right_brace|.
  2138. @d aux_err_stuff_after_right_brace == begin
  2139.                       aux_err_stuff_after_right_brace_print;
  2140.                       aux_err_return;
  2141.                       end
  2142. @<Procedures and functions for all file I/O, error messages, and such@>=
  2143. procedure aux_err_stuff_after_right_brace_print;
  2144. begin
  2145. print ('Stuff after "',xchr[right_brace],'"');
  2146. And this one complains when a command has |white_space| in its
  2147. argument.
  2148. @d aux_err_white_space_in_argument == begin
  2149.                       aux_err_white_space_in_argument_print;
  2150.                       aux_err_return;
  2151.                       end
  2152. @<Procedures and functions for all file I/O, error messages, and such@>=
  2153. procedure aux_err_white_space_in_argument_print;
  2154. begin
  2155. print ('White space in argument');
  2156. @^auxiliary-file commands@>
  2157. @:this can't happen}{\quad Unknown auxiliary-file command@>
  2158. We're not at the end of an \.{.aux} file, so we see if the current
  2159. line might be a command of interest.  A command of interest will be a
  2160. line without blanks, consisting of a command name, a |left_brace|, one
  2161. or more arguments separated by commas, and a |right_brace|.
  2162. @<Scan for and process an \.{.aux} command@>=
  2163. procedure get_aux_command_and_process;
  2164. label exit;
  2165. begin
  2166. buf_ptr2 := 0;                {mark the beginning of the next token}
  2167. if (not scan1(left_brace)) then        {no |left_brace|---flush line}
  2168.     return;
  2169. command_num := ilk_info[
  2170.     str_lookup(buffer,buf_ptr1,token_len,aux_command_ilk,dont_insert)];
  2171. if (hash_found) then
  2172.     case (command_num) of
  2173.     n_aux_bibdata : aux_bib_data_command;
  2174.     n_aux_bibstyle : aux_bib_style_command;
  2175.     n_aux_citation : aux_citation_command;
  2176.     n_aux_input : aux_input_command;
  2177.     othercases
  2178.         confusion ('Unknown auxiliary-file command')
  2179.     endcases;
  2180. exit:
  2181. Here we introduce some variables for processing a \.{\\bibdata}
  2182. command.  Each element in |bib_list| (except for
  2183. |bib_list[max_bib_files]|, which is always unused) is a pointer to the
  2184. appropriate |str_pool| string representing the \.{.bib} file name.
  2185. The array |bib_file| contains the corresponding \PASCAL\ |file|
  2186. variables.
  2187. @d cur_bib_str == bib_list[bib_ptr]    {shorthand for current \.{.bib} file}
  2188. @d cur_bib_file == bib_file[bib_ptr]    {shorthand for current |bib_file|}
  2189. @<Globals in the outer block@>=
  2190. @!bib_list : array[bib_number] of str_number;    {the \.{.bib} file list}
  2191. @!bib_ptr : bib_number;        {pointer for the current \.{.bib} file}
  2192. @!num_bib_files : bib_number;    {the total number of \.{.bib} files}
  2193. @!bib_seen : boolean;    {|true| if we've already seen a \.{\\bibdata} command}
  2194. @!bib_file : array[bib_number] of alpha_file; {corresponding |file| variables}
  2195. Where |bib_number| is the obvious.
  2196. @<Types in the outer block@>=
  2197. @!bib_number = 0..max_bib_files;    {gives the |bib_list| range}
  2198. @<Set initial values of key variables@>=
  2199. bib_ptr := 0;        {this makes |bib_list| empty}
  2200. bib_seen := false;    {we haven't seen a \.{\\bibdata} command yet}
  2201. @:auxiliary-file commands}{\quad \.{\\bibdata}@>
  2202. A \.{\\bibdata} command will have its arguments between braces and
  2203. separated by commas.  There must be exactly one such command in the
  2204. \.{.aux} file(s).  All upper-case letters are converted to lower case.
  2205. @<Procedures and functions for the reading and processing of input files@>=
  2206. procedure aux_bib_data_command;
  2207. label exit;
  2208. begin
  2209. if (bib_seen) then
  2210.     aux_err_illegal_another (n_aux_bibdata);
  2211. bib_seen := true;    {now we've seen a \.{\\bibdata} command}
  2212. while (scan_char <> right_brace) do
  2213.     begin
  2214.     incr(buf_ptr2);            {skip over the previous stop-character}
  2215.     if (not scan2_white(right_brace,comma)) then
  2216.     aux_err_no_right_brace;
  2217.     if (lex_class[scan_char] = white_space) then
  2218.     aux_err_white_space_in_argument;
  2219.     if ((last > buf_ptr2+1) and (scan_char = right_brace)) then
  2220.     aux_err_stuff_after_right_brace;
  2221.     @<Open a \.{.bib} file@>;
  2222.     end;
  2223. exit:
  2224. Here's a procedure we'll need shortly.  It prints the name of the
  2225. current \.{.bib} file, followed by a |newline|.
  2226. @<Procedures and functions for all file I/O, error messages, and such@>=
  2227. procedure print_bib_name;
  2228. begin
  2229. print_pool_str (cur_bib_str);
  2230. print_pool_str (s_bib_extension);
  2231. print_newline;
  2232. This macro is similar to |aux_err| but it complains specifically about
  2233. opening a file for a \.{\\bibdata} command.
  2234. @d open_bibdata_aux_err(#) == begin
  2235.                   print (#);
  2236.                   print_bib_name;
  2237.                   aux_err_return;    {this does the |mark_error|}
  2238.                   end
  2239. @:BibTeX capacity exceeded}{\quad number of \.{.bib} files@>
  2240. Now we add the just-found argument to |bib_list| if it hasn't already
  2241. been encountered as a \.{\\bibdata} argument and if, after appending
  2242. the |s_bib_extension| string, the resulting file name can be opened.
  2243. @<Open a \.{.bib} file@>=
  2244. begin
  2245. if (bib_ptr = max_bib_files) then
  2246.     overflow('number of database files ',max_bib_files);
  2247. cur_bib_str := hash_text[
  2248.         str_lookup(buffer,buf_ptr1,token_len,bib_file_ilk,do_insert)];
  2249. if (hash_found) then    {already encountered this as a \.{\\bibdata} argument}
  2250.     open_bibdata_aux_err ('This database file appears more than once: ');
  2251. start_name (cur_bib_str);
  2252. add_extension (s_bib_extension);
  2253. if (not a_open_in(cur_bib_file)) then
  2254.     begin
  2255.     add_area (s_bib_area);
  2256.     if (not a_open_in(cur_bib_file)) then
  2257.     open_bibdata_aux_err ('I couldn''t open database file ');
  2258.     end;
  2259.   trace
  2260.   trace_pr_pool_str (cur_bib_str);
  2261.   trace_pr_pool_str (s_bib_extension);
  2262.   trace_pr_ln (' is a bibdata file');
  2263.   ecart@/
  2264. incr(bib_ptr);
  2265. Here we introduce some variables for processing a \.{\\bibstyle}
  2266. command.
  2267. @<Globals in the outer block@>=
  2268. @!bst_seen : boolean;    {|true| if we've already seen a \.{\\bibstyle} command}
  2269. @!bst_str : str_number;        {the string number for the \.{.bst} file}
  2270. @!bst_file : alpha_file;    {the corresponding |file| variable}
  2271. And we initialize.
  2272. @<Set initial values of key variables@>=
  2273. bst_str := 0;        {mark |bst_str| as unused}
  2274. bst_seen := false;    {we haven't seen a \.{\\bibstyle} command yet}
  2275. @:auxiliary-file commands}{\quad \.{\\bibstyle}@>
  2276. A \.{\\bibstyle} command will have exactly one argument, and it will
  2277. be between braces.  There must be exactly one such command in the
  2278. \.{.aux} file(s).  All upper-case letters are converted to lower case.
  2279. @<Procedures and functions for the reading and processing of input files@>=
  2280. procedure aux_bib_style_command;
  2281. label exit;
  2282. begin
  2283. if (bst_seen) then
  2284.     aux_err_illegal_another (n_aux_bibstyle);
  2285. bst_seen := true;        {now we've seen a \.{\\bibstyle} command}
  2286. incr(buf_ptr2);            {skip over the |left_brace|}
  2287. if (not scan1_white(right_brace)) then
  2288.     aux_err_no_right_brace;
  2289. if (lex_class[scan_char] = white_space) then
  2290.     aux_err_white_space_in_argument;
  2291. if (last > buf_ptr2+1) then
  2292.     aux_err_stuff_after_right_brace;
  2293. @<Open the \.{.bst} file@>;
  2294. exit:
  2295. @:this can't happen}{\quad Already encountered style file@>
  2296. Now we open the file whose name is the just-found argument appended
  2297. with the |s_bst_extension| string, if possible.
  2298. @<Open the \.{.bst} file@>=
  2299. begin
  2300. bst_str := hash_text[
  2301.         str_lookup(buffer,buf_ptr1,token_len,bst_file_ilk,do_insert)];
  2302. if (hash_found) then
  2303.     begin
  2304.       trace
  2305.       print_bst_name;
  2306.       ecart@/
  2307.     confusion ('Already encountered style file');
  2308.     end;
  2309. start_name (bst_str);
  2310. add_extension (s_bst_extension);
  2311. if (not a_open_in(bst_file)) then
  2312.     begin
  2313.     add_area (s_bst_area);
  2314.     if (not a_open_in(bst_file)) then
  2315.     begin
  2316.     print ('I couldn''t open style file ');
  2317.     print_bst_name;@/
  2318.     bst_str := 0;                {mark as unused again}
  2319.     aux_err_return;
  2320.     end;
  2321.     end;
  2322. print ('The style file: ');
  2323. print_bst_name;
  2324. Print the name of the \.{.bst} file, followed by a |newline|.
  2325. @<Procedures and functions for all file I/O, error messages, and such@>=
  2326. procedure print_bst_name;
  2327. begin
  2328. print_pool_str (bst_str);
  2329. print_pool_str (s_bst_extension);
  2330. print_newline;
  2331. Here we introduce some variables for processing a \.{\\citation}
  2332. command.  Each element in |cite_list| (except for
  2333. |cite_list[max_cites]|, which is always unused) is a pointer to the
  2334. appropriate |str_pool| string.  The cite-key list is kept in order of
  2335. occurrence with duplicates removed.
  2336. @d cur_cite_str == cite_list[cite_ptr]    {shorthand for the current cite key}
  2337. @<Globals in the outer block@>=
  2338. @!cite_list : packed array[cite_number] of str_number;    {the cite-key list}
  2339. @!cite_ptr : cite_number;    {pointer for the current cite key}
  2340. @!entry_cite_ptr : cite_number;    {cite pointer for the current entry}
  2341. @!num_cites : cite_number;    {the total number of distinct cite keys}
  2342. @!old_num_cites : cite_number;    {set to a previous |num_cites| value}
  2343. @!citation_seen : boolean;    {|true| if we've seen a \.{\\citation} command}
  2344. @!cite_loc : hash_loc;        {the hash-table location of a cite key}
  2345. @!lc_cite_loc : hash_loc;    {and of its lower-case equivalent}
  2346. @!lc_xcite_loc : hash_loc;    {a second |lc_cite_loc| variable}
  2347. @!cite_found : boolean;        {|true| if we've already seen this cite key}
  2348. @!all_entries : boolean;    {|true| if we're to use the entire database}
  2349. @!all_marker : cite_number;    {we put the other entries in |cite_list| here}
  2350. Where |cite_number| is the obvious.
  2351. @<Types in the outer block@>=
  2352. @!cite_number = 0..max_cites;    {gives the |cite_list| range}
  2353. @<Set initial values of key variables@>=
  2354. cite_ptr := 0;        {this makes |cite_list| empty}
  2355. citation_seen := false;    {we haven't seen a \.{\\citation} command yet}
  2356. all_entries := false;    {by default, use just the entries explicitly named}
  2357. @^case mismatch@>
  2358. @^entire database inclusion@>
  2359. @^whole database inclusion@>
  2360. @:LaTeX}{\LaTeX@>
  2361. @:auxiliary-file commands}{\quad \.{\\citation}@>
  2362. A \.{\\citation} command will have its arguments between braces and
  2363. separated by commas.  Upper/lower cases are considered to be different
  2364. for \.{\\citation} arguments, which is the same as the rest of \LaTeX\
  2365. but different from the rest of \BibTeX.  A cite key needn't exactly
  2366. case-match its corresponding database key to work, although two cite
  2367. keys that are case-mismatched will produce an error message.
  2368. (A {\sl case mismatch\/} is a mismatch, but only because of a case
  2369. difference.)
  2370. A \.{\\citation} command having \.{*} as an argument indicates that
  2371. the entire database will be included (almost as if a \.{\\nocite}
  2372. command that listed every cite key in the database, in order, had been
  2373. given at the corresponding spot in the \.{.tex} file).
  2374. @d next_cite = 23    {read the next argument}
  2375. @<Procedures and functions for the reading and processing of input files@>=
  2376. procedure aux_citation_command;
  2377. label next_cite,@!exit;
  2378. begin
  2379. citation_seen := true;        {now we've seen a \.{\\citation} command}
  2380. while (scan_char <> right_brace) do
  2381.     begin
  2382.     incr(buf_ptr2);        {skip over the previous stop-character}
  2383.     if (not scan2_white(right_brace,comma)) then
  2384.     aux_err_no_right_brace;
  2385.     if (lex_class[scan_char] = white_space) then
  2386.     aux_err_white_space_in_argument;
  2387.     if ((last > buf_ptr2+1) and (scan_char = right_brace)) then
  2388.     aux_err_stuff_after_right_brace;
  2389.     @<Check the cite key@>;
  2390. next_cite:
  2391.     end;
  2392. exit:
  2393. @^kludge@>
  2394. We must check if (the lower-case version of) this cite key has been
  2395. previously encountered, and proceed accordingly.  The alias kludge
  2396. helps make the stack space not overflow on some machines.
  2397. @d ex_buf1== ex_buf        {an alias, used only in this module}
  2398. @<Check the cite key@>=
  2399. begin
  2400.   trace
  2401.   trace_pr_token;
  2402.   trace_pr (' cite key encountered');
  2403.   ecart@/
  2404. @<Check for entire database inclusion (and thus skip this cite key)@>;
  2405. tmp_ptr := buf_ptr1;
  2406. while (tmp_ptr < buf_ptr2) do
  2407.     begin
  2408.     ex_buf1[tmp_ptr] := buffer[tmp_ptr];
  2409.     incr(tmp_ptr);
  2410.     end;
  2411. lower_case (ex_buf1, buf_ptr1, token_len);    {convert to `canonical' form}
  2412. lc_cite_loc := str_lookup(ex_buf1,buf_ptr1,token_len,lc_cite_ilk,do_insert);
  2413. if (hash_found) then    {already encountered this as a \.{\\citation} argument}
  2414.     @<Cite seen, don't add a cite key@>
  2415.   else
  2416.     @<Cite unseen, add a cite key@>;
  2417.                 {it's a new cite key---add it to |cite_list|}
  2418. Here we check for a \.{\\citation} command having \.{*} as an
  2419. argument, indicating that the entire database will be included.
  2420. @<Check for entire database inclusion (and thus skip this cite key)@>=
  2421. begin
  2422. if (token_len = 1) then
  2423.   if (buffer[buf_ptr1] = star) then
  2424.     begin
  2425.       trace
  2426.       trace_pr_ln ('---entire database to be included');
  2427.       ecart@/
  2428.     if (all_entries) then
  2429.     begin
  2430.     print_ln ('Multiple inclusions of entire database');
  2431.     aux_err_return;
  2432.       else
  2433.     begin
  2434.     all_entries := true;
  2435.     all_marker := cite_ptr;
  2436.     goto next_cite;
  2437.     end;
  2438.     end;
  2439. @^case mismatch errors@>
  2440. We've previously encountered the lower-case version, so we check that
  2441. the actual version exactly matches the actual version of the
  2442. previously-encountered cite key(s).
  2443. @<Cite seen, don't add a cite key@>=
  2444. begin
  2445.   trace
  2446.   trace_pr_ln (' previously');
  2447.   ecart@/
  2448. dummy_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,dont_insert);
  2449. if (not hash_found) then        {case mismatch error}
  2450.     begin
  2451.     print ('Case mismatch error between cite keys ');
  2452.     print_token;
  2453.     print (' and ');
  2454.     print_pool_str (cite_list[ilk_info[ilk_info[lc_cite_loc]]]);
  2455.     print_newline;
  2456.     aux_err_return;
  2457.     end;
  2458. @:this can't happen}{\quad Cite hash error@>
  2459. Now we add the just-found argument to |cite_list| if there isn't
  2460. anything funny happening.
  2461. @<Cite unseen, add a cite key@>=
  2462. begin
  2463.   trace
  2464.   trace_pr_newline;
  2465.   ecart@/
  2466. cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
  2467. if (hash_found) then
  2468.     hash_cite_confusion;
  2469. check_cite_overflow (cite_ptr);
  2470. cur_cite_str := hash_text[cite_loc];
  2471. ilk_info[cite_loc] := cite_ptr;
  2472. ilk_info[lc_cite_loc] := cite_loc;
  2473. incr(cite_ptr);
  2474. @:this can't happen}{\quad Cite hash error@>
  2475. Here's a serious complaint (that is, a bug) concerning hash problems.
  2476. This is the first of several similar bug-procedures that exist only
  2477. because they save space.
  2478. @<Procedures and functions for all file I/O, error messages, and such@>=
  2479. procedure hash_cite_confusion;
  2480. begin
  2481. confusion ('Cite hash error');
  2482. @^fetish@>
  2483. @:BibTeX capacity exceeded}{\quad number of cite keys@>
  2484. Complain if somebody's got a cite fetish.  This procedure is called
  2485. when were about to add another cite key to |cite_list|.  It assumes
  2486. that |cite_loc| gives the potential cite key's hash table location.
  2487. @<Procedures and functions for all file I/O, error messages, and such@>=
  2488. procedure check_cite_overflow (@!last_cite : cite_number);
  2489. begin
  2490. if (last_cite = max_cites) then
  2491.     begin
  2492.     print_pool_str (hash_text[cite_loc]);
  2493.     print_ln (' is the key:');
  2494.     overflow('number of cite keys ',max_cites);
  2495.     end;
  2496. @:auxiliary-file commands}{\quad \.{\\\AT!input}@>
  2497. An \.{\\@@input} command will have exactly one argument, it will
  2498. be between braces, and it must have the |s_aux_extension|.
  2499. All upper-case letters are converted to lower case.
  2500. @<Procedures and functions for the reading and processing of input files@>=
  2501. procedure aux_input_command;
  2502. label exit;
  2503. var aux_extension_ok : boolean;        {to check for a correct file extension}
  2504. begin
  2505. incr(buf_ptr2);                {skip over the |left_brace|}
  2506. if (not scan1_white(right_brace)) then
  2507.     aux_err_no_right_brace;
  2508. if (lex_class[scan_char] = white_space) then
  2509.     aux_err_white_space_in_argument;
  2510. if (last > buf_ptr2+1) then
  2511.     aux_err_stuff_after_right_brace;
  2512. @<Push the \.{.aux} stack@>;
  2513. exit:
  2514. @:BibTeX capacity exceeded}{\quad number of \.{.aux} files@>
  2515. We must check that this potential \.{.aux} file won't overflow the
  2516. stack, that it has the correct extension, that we haven't encountered
  2517. it before (to prevent, among other things, an infinite loop).
  2518. @<Push the \.{.aux} stack@>=
  2519. begin
  2520. incr(aux_ptr);
  2521. if (aux_ptr = aux_stack_size) then
  2522.     begin
  2523.     print_token; print (': ');
  2524.     overflow('auxiliary file depth ',aux_stack_size);
  2525.     end;
  2526. aux_extension_ok := true;
  2527. if (token_len < length(s_aux_extension)) then@/
  2528.     aux_extension_ok := false    {else |str_eq_buf| might bomb the program}
  2529. else if (not str_eq_buf(s_aux_extension, buffer,
  2530.     buf_ptr2-length(s_aux_extension), length(s_aux_extension))) then
  2531.     aux_extension_ok := false;
  2532. if (not aux_extension_ok) then
  2533.     begin
  2534.     print_token;
  2535.     print (' has a wrong extension');
  2536.     decr(aux_ptr);
  2537.     aux_err_return;
  2538.     end;
  2539. cur_aux_str := hash_text[
  2540.         str_lookup(buffer,buf_ptr1,token_len,aux_file_ilk,do_insert)];
  2541. if (hash_found) then
  2542.     begin
  2543.     print ('Already encountered file ');
  2544.     print_aux_name;
  2545.     decr(aux_ptr);
  2546.     aux_err_return;
  2547.     end;
  2548. @<Open this \.{.aux} file@>;
  2549. We check that this \.{.aux} file can actually be opened, and then open it.
  2550. @<Open this \.{.aux} file@>=
  2551. begin
  2552. start_name (cur_aux_str);    {extension already there for \.{.aux} files}
  2553. name_ptr := name_length+1;
  2554. while (name_ptr <= file_name_size) do    {pad with blanks}
  2555.     begin
  2556.     name_of_file[name_ptr] := ' ';
  2557.     incr(name_ptr);
  2558.     end;
  2559. if (not a_open_in(cur_aux_file)) then
  2560.     begin
  2561.     print ('I couldn''t open auxiliary file ');
  2562.     print_aux_name;
  2563.     decr(aux_ptr);
  2564.     aux_err_return;
  2565.     end;
  2566. print ('A level-',aux_ptr:0,' auxiliary file: ');
  2567. print_aux_name;
  2568. cur_aux_line := 0;
  2569. Here we close the current-level \.{.aux} file and go back up a level,
  2570. if possible, by decrementing |aux_ptr|.
  2571. @<Procedures and functions for the reading and processing of input files@>=
  2572. procedure pop_the_aux_stack;
  2573. begin
  2574. a_close (cur_aux_file);
  2575. if (aux_ptr=0) then
  2576.     goto aux_done
  2577.   else
  2578.     decr(aux_ptr);
  2579. @^gymnastics@>
  2580. That's it for processing \.{.aux} commands, except for finishing the
  2581. procedural gymnastics.
  2582. @<Procedures and functions for the reading and processing of input files@>=
  2583. @<Scan for and process an \.{.aux} command@>
  2584. We must complain if anything's amiss.
  2585. @d aux_end_err(#) == begin
  2586.              aux_end1_err_print;
  2587.              print (#);
  2588.              aux_end2_err_print;
  2589.              end
  2590. @<Procedures and functions for all file I/O, error messages, and such@>=
  2591. procedure aux_end1_err_print;
  2592. begin
  2593. print ('I found no ');
  2594. procedure aux_end2_err_print;
  2595. begin
  2596. print ('---while reading file ');
  2597. print_aux_name;
  2598. mark_error;
  2599. Before proceeding, we see if we have any complaints.
  2600. @<Procedures and functions for the reading and processing of input files@>=
  2601. procedure last_check_for_aux_errors;
  2602. begin
  2603. num_cites := cite_ptr;        {record the number of distinct cite keys}
  2604. num_bib_files := bib_ptr;    {and the number of \.{.bib} files}
  2605. if (not citation_seen) then
  2606.     aux_end_err ('\citation commands')
  2607.   else if ((num_cites = 0) and (not all_entries)) then
  2608.     aux_end_err ('cite keys');
  2609. if (not bib_seen) then
  2610.     aux_end_err ('\bibdata command')
  2611.   else if (num_bib_files = 0) then
  2612.     aux_end_err ('database files');
  2613. if (not bst_seen) then
  2614.     aux_end_err ('\bibstyle command')
  2615.   else if (bst_str = 0) then
  2616.     aux_end_err ('style file');
  2617. @* Reading the style file.
  2618. This part of the program reads the \.{.bst} file, which consists of a
  2619. sequence of commands.  Each \.{.bst} command consists of a name (for
  2620. which case differences are ignored) followed by zero or more
  2621. arguments, each enclosed in braces.
  2622. @d bst_done=32        {go here when finished with the \.{.bst} file}
  2623. @d no_bst_file=9932    {go here when skipping the \.{.bst} file}
  2624. @<Labels in the outer block@>=
  2625. ,@!bst_done,@!no_bst_file
  2626. The |bbl_line_num| gets initialized along with the |bst_line_num|, so
  2627. it's declared here too.
  2628. @<Globals in the outer block@>=
  2629. @!bbl_line_num : integer;    {line number of the \.{.bbl} (output) file}
  2630. @!bst_line_num : integer;    {line number of the \.{.bst} file}
  2631. This little procedure exists because it's used by at least two other
  2632. procedures and thus saves some space.
  2633. @<Procedures and functions for all file I/O, error messages, and such@>=
  2634. procedure bst_ln_num_print;
  2635. begin
  2636. print ('--line ',bst_line_num:0,' of file ');
  2637. print_bst_name;
  2638. When there's a serious error parsing the \.{.bst} file, we flush the
  2639. rest of the current command; a blank line is assumed to mark the end
  2640. of a command (but for the purposes of error recovery only).  Thus,
  2641. error recovery will be better if style designers leave blank lines
  2642. between \.{.bst} commands.  This macro must be called from within a
  2643. procedure that has an |exit| label.
  2644. @d bst_err_print_and_look_for_blank_line_return ==
  2645.         begin
  2646.         bst_err_print_and_look_for_blank_line;
  2647.         return;
  2648.         end
  2649. @d bst_err(#) == begin        {serious error during \.{.bst} parsing}
  2650.          print (#);
  2651.          bst_err_print_and_look_for_blank_line_return;
  2652.          end
  2653. @<Procedures and functions for all file I/O, error messages, and such@>=
  2654. procedure bst_err_print_and_look_for_blank_line;
  2655. begin
  2656. print ('-');
  2657. bst_ln_num_print;
  2658. print_bad_input_line;            {this call does the |mark_error|}
  2659. while (last <> 0) do            {look for a blank input line}
  2660.     if (not input_ln(bst_file)) then    {or the end of the file}
  2661.     goto bst_done
  2662.       else
  2663.     incr(bst_line_num);
  2664. buf_ptr2 := last;            {to input the next line}
  2665. When there's a harmless error parsing the \.{.bst} file (harmless
  2666. syntactically, at least) we give just a |warning_message|.
  2667. @d bst_warn(#) == begin        {non-serious error during \.{.bst} parsing}
  2668.           print (#);
  2669.           bst_warn_print;
  2670.           end
  2671. @<Procedures and functions for all file I/O, error messages, and such@>=
  2672. procedure bst_warn_print;
  2673. begin
  2674. bst_ln_num_print;
  2675. mark_warning;
  2676. Here's the outer loop for reading the \.{.bst} file---it keeps reading
  2677. and processing \.{.bst} commands until none left.  This is part of the
  2678. main program; hence, because of the |bst_done| label, there's no
  2679. conventional |begin|-|end| pair surrounding the entire module.
  2680. @<Read and execute the \.{.bst} file@>=
  2681. if (bst_str = 0) then    {there's no \.{.bst} file to read}
  2682.     goto no_bst_file;    {this is a |goto| so that |bst_done| is not in a block}
  2683. bst_line_num := 0;    {initialize things}
  2684. bbl_line_num := 1;    {best spot to initialize the output line number}
  2685. buf_ptr2 := last;    {to get the first input line}
  2686.     begin
  2687.     if (not eat_bst_white_space) then    {the end of the \.{.bst} file}
  2688.     goto bst_done;
  2689.     get_bst_command_and_process;
  2690.     end;
  2691. bst_done: a_close (bst_file);
  2692. no_bst_file: a_close (bbl_file);
  2693. This \.{.bst}-specific scanning function skips over |white_space|
  2694. characters (and comments) until hitting a nonwhite character or the
  2695. end of the file, respectively returning |true| or |false|.  It also
  2696. updates |bst_line_num|, the line counter.
  2697. @<Procedures and functions for input scanning@>=
  2698. function eat_bst_white_space : boolean;
  2699. label exit;
  2700. begin
  2701.     begin
  2702.     if (scan_white_space) then        {hit a nonwhite character on this line}
  2703.     if (scan_char <> comment) then    {it's not a comment character; return}
  2704.         begin
  2705.         eat_bst_white_space := true;
  2706.         return;
  2707.         end;
  2708.     if (not input_ln(bst_file)) then    {end-of-file; return |false|}
  2709.     begin
  2710.     eat_bst_white_space := false;
  2711.     return;
  2712.     end;
  2713.     incr(bst_line_num);
  2714.     buf_ptr2 := 0;
  2715.     end;
  2716. exit:
  2717. It's often illegal to end a \.{.bst} command in certain places, and
  2718. this is where we come to check.
  2719. @d eat_bst_white_and_eof_check(#) ==
  2720.     begin
  2721.     if (not eat_bst_white_space) then
  2722.         begin
  2723.         eat_bst_print;
  2724.         bst_err (#);
  2725.         end;
  2726. @<Procedures and functions for all file I/O, error messages, and such@>=
  2727. procedure eat_bst_print;
  2728. begin
  2729. print ('Illegal end of style file in command: ');
  2730. We must attend to a few details before getting to work on this
  2731. \.{.bst} command.
  2732. @<Scan for and process a \.{.bst} command@>=
  2733. procedure get_bst_command_and_process;
  2734. label exit;
  2735. begin
  2736. if (not scan_alpha) then
  2737.     bst_err ('"',xchr[scan_char],'" can''t start a style-file command');
  2738. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  2739. command_num := ilk_info[
  2740.     str_lookup(buffer,buf_ptr1,token_len,bst_command_ilk,dont_insert)];
  2741. if (not hash_found) then
  2742.     begin
  2743.     print_token;
  2744.     bst_err (' is an illegal style-file command');
  2745.     end;
  2746. @<Process the appropriate \.{.bst} command@>;
  2747. exit:
  2748. @^style-file commands@>
  2749. @:this can't happen}{\quad Unknown style-file command@>
  2750. Here we determine which \.{.bst} command we're about to process, and
  2751. then go to it.
  2752. @<Process the appropriate \.{.bst} command@>=
  2753. case (command_num) of
  2754.     n_bst_entry : bst_entry_command;
  2755.     n_bst_execute : bst_execute_command;
  2756.     n_bst_function : bst_function_command;
  2757.     n_bst_integers : bst_integers_command;
  2758.     n_bst_iterate : bst_iterate_command;
  2759.     n_bst_macro : bst_macro_command;
  2760.     n_bst_read : bst_read_command;
  2761.     n_bst_reverse : bst_reverse_command;
  2762.     n_bst_sort : bst_sort_command;
  2763.     n_bst_strings : bst_strings_command;
  2764.     othercases confusion ('Unknown style-file command')
  2765. endcases
  2766. We need data structures for the function definitions, the entry
  2767. variables, the global variables, and the actual entries corresponding
  2768. to the cite-key list.  First we define the classes of `function's
  2769. used.  Functions in all classes are of |bst_fn_ilk| except for
  2770. |int_literal|s, which are of |integer_ilk|; and |str_literal|s, which
  2771. are of |text_ilk|.
  2772. @d built_in = 0        {the `primitive' functions}
  2773. @d wiz_defined = 1    {defined in the \.{.bst} file}
  2774. @d int_literal = 2    {integer `constants'}
  2775. @d str_literal = 3    {string `constants'}
  2776. @d field = 4        {things like `author' and `title'}
  2777. @d int_entry_var = 5    {integer entry variable}
  2778. @d str_entry_var = 6    {string entry variable}
  2779. @d int_global_var = 7    {integer global variable}
  2780. @d str_global_var = 8    {string global variable}
  2781. @d last_fn_class = 8    {the same number as on the line above}
  2782. @:this can't happen}{\quad Unknown function class@>
  2783. Here's another bug report.
  2784. @<Procedures and functions for all file I/O, error messages, and such@>=
  2785. procedure unknwn_function_class_confusion;
  2786. begin
  2787. confusion ('Unknown function class');
  2788. @:this can't happen}{\quad Unknown function class@>
  2789. Occasionally we'll want to |print| the name of one of these function
  2790. classes.
  2791. @<Procedures and functions for all file I/O, error messages, and such@>=
  2792. procedure print_fn_class (@!fn_loc : hash_loc);
  2793. begin
  2794. case (fn_type[fn_loc]) of
  2795.     built_in : print ('built-in');
  2796.     wiz_defined : print ('wizard-defined');
  2797.     int_literal : print ('integer-literal');
  2798.     str_literal : print ('string-literal');
  2799.     field : print ('field');
  2800.     int_entry_var : print ('integer-entry-variable');
  2801.     str_entry_var : print ('string-entry-variable');
  2802.     int_global_var : print ('integer-global-variable');
  2803.     str_global_var : print ('string-global-variable');
  2804.     othercases unknwn_function_class_confusion
  2805. endcases;
  2806. @:this can't happen}{\quad Unknown function class@>
  2807. This version is for printing when in |trace| mode.
  2808. @<Procedures and functions for all file I/O, error messages, and such@>=
  2809.   trace
  2810.   procedure trace_pr_fn_class (@!fn_loc : hash_loc);
  2811.   begin
  2812.   case (fn_type[fn_loc]) of
  2813.     built_in : trace_pr ('built-in');
  2814.     wiz_defined : trace_pr ('wizard-defined');
  2815.     int_literal : trace_pr ('integer-literal');
  2816.     str_literal : trace_pr ('string-literal');
  2817.     field : trace_pr ('field');
  2818.     int_entry_var : trace_pr ('integer-entry-variable');
  2819.     str_entry_var : trace_pr ('string-entry-variable');
  2820.     int_global_var : trace_pr ('integer-global-variable');
  2821.     str_global_var : trace_pr ('string-global-variable');
  2822.     othercases unknwn_function_class_confusion
  2823.   endcases;
  2824.   end;
  2825.   ecart
  2826. Besides the function classes, we have types based on \BibTeX's
  2827. capacity limitations and one based on what can go into the array
  2828. |wiz_functions| explained below.
  2829. @d quote_next_fn = hash_base - 1  {special marker used in defining functions}
  2830. @d end_of_def = hash_max + 1      {another such special marker}
  2831. @<Types in the outer block@>=
  2832. @!fn_class = 0..last_fn_class;        {the \.{.bst} function classes}
  2833. @!wiz_fn_loc = 0..wiz_fn_space;     {|wiz_defined|-function storage locations}
  2834. @!int_ent_loc = 0..max_ent_ints;    {|int_entry_var| storage locations}
  2835. @!str_ent_loc = 0..max_ent_strs;    {|str_entry_var| storage locations}
  2836. @!str_glob_loc = 0..max_glb_str_minus_1; {|str_global_var| storage locations}
  2837. @!field_loc = 0..max_fields;        {individual field storage locations}
  2838. @!hash_ptr2 = quote_next_fn..end_of_def; {a special marker or a |hash_loc|}
  2839. @^save space@>
  2840. @^space savings@>
  2841. @^system dependencies@>
  2842. We store information about the \.{.bst} functions in arrays the same
  2843. size as the hash-table arrays and in locations corresponding to their
  2844. hash-table locations.  The two arrays |fn_info| (an alias of
  2845. |ilk_info| described earlier) and |fn_type| accomplish this: |fn_type|
  2846. specifies one of the above classes, and |fn_info| gives information
  2847. dependent on the class.
  2848. Six other arrays give the contents of functions: The array
  2849. |wiz_functions| holds definitions for |wiz_defined| functions---each
  2850. such function consists of a sequence of pointers to hash-table
  2851. locations of other functions (with the two special-marker exceptions
  2852. above); the array |entry_ints| contains the current values of
  2853. |int_entry_var|s; the array |entry_strs| contains the current values
  2854. of |str_entry_var|s; an element of the array |global_strs| contains
  2855. the current value of a |str_global_var| if the corresponding
  2856. |glb_str_ptr| entry is empty, otherwise the nonempty entry is a
  2857. pointer to the string; and the array |field_info|, for each field of
  2858. each entry, contains either a pointer to the string or the special
  2859. value |missing|.
  2860. The array |global_strs| isn't packed (that is, it isn't |array| \dots\
  2861. |of packed array| \dots$\,$) to increase speed on some systems;
  2862. however, on systems that are byte-addressable and that have a good
  2863. compiler, packing |global_strs| would save lots of space without much
  2864. loss of speed.
  2865. @d fn_info == ilk_info        {an alias used with functions}
  2866. @d missing = empty        {a special pointer for missing fields}
  2867. @<Globals in the outer block@>=
  2868. @!fn_loc : hash_loc;        {the hash-table location of a function}
  2869. @!wiz_loc : hash_loc;        {the hash-table location of a wizard function}
  2870. @!literal_loc : hash_loc;    {the hash-table location of a literal function}
  2871. @!macro_name_loc : hash_loc;    {the hash-table location of a macro name}
  2872. @!macro_def_loc : hash_loc;    {the hash-table location of a macro definition}
  2873. @!fn_type : packed array[hash_loc] of fn_class;
  2874. @!wiz_def_ptr : wiz_fn_loc;    {storage location for the next wizard function}
  2875. @!wiz_fn_ptr : wiz_fn_loc;    {general |wiz_functions| location}
  2876. @!wiz_functions : packed array[wiz_fn_loc] of hash_ptr2;
  2877. @!int_ent_ptr : int_ent_loc;    {general |int_entry_var| location}
  2878. @!entry_ints : array[int_ent_loc] of integer;
  2879. @!num_ent_ints : int_ent_loc;    {the number of distinct |int_entry_var| names}
  2880. @!str_ent_ptr : str_ent_loc;    {general |str_entry_var| location}
  2881. @!entry_strs : array[str_ent_loc] of
  2882.                 packed array[0..ent_str_size] of ASCII_code;
  2883. @!num_ent_strs : str_ent_loc;    {the number of distinct |str_entry_var| names}
  2884. @!str_glb_ptr : 0..max_glob_strs;    {general |str_global_var| location}
  2885. @!glb_str_ptr : array[str_glob_loc] of str_number;
  2886. @!global_strs : array[str_glob_loc] of array[0..glob_str_size] of ASCII_code;
  2887. @!glb_str_end : array[str_glob_loc] of 0..glob_str_size;    {end markers}
  2888. @!num_glb_strs : 0..max_glob_strs; {number of distinct |str_global_var| names}
  2889. @!field_ptr : field_loc;    {general |field_info| location}
  2890. @!field_parent_ptr,@!field_end_ptr : field_loc; {two more for doing cross-refs}
  2891. @!cite_parent_ptr,@!cite_xptr : cite_number;  {two others for doing cross-refs}
  2892. @!field_info : packed array[field_loc] of str_number;
  2893. @!num_fields : field_loc;    {the number of distinct field names}
  2894. @!num_pre_defined_fields : field_loc;    {so far, just one: \.{crossref}}
  2895. @!crossref_num : field_loc;    {the number given to \.{crossref}}
  2896. @!no_fields : boolean;        {used for |tr_print|ing entry information}
  2897. Now we initialize storage for the |wiz_defined| functions and we
  2898. initialize variables so that the first |str_entry_var|,
  2899. |int_entry_var|, |str_global_var|, and |field| name will be assigned
  2900. the number~0.  Note: The variables |num_ent_strs| and |num_fields|
  2901. will also be set when pre-defining strings.
  2902. @<Set initial values of key variables@>=
  2903. wiz_def_ptr := 0;
  2904. num_ent_ints := 0;
  2905. num_ent_strs := 0;
  2906. num_fields := 0;
  2907. str_glb_ptr := 0;
  2908. while (str_glb_ptr < max_glob_strs) do        {make |str_global_var|s empty}
  2909.     begin
  2910.     glb_str_ptr[str_glb_ptr] := 0;
  2911.     glb_str_end[str_glb_ptr] := 0;
  2912.     incr(str_glb_ptr);
  2913.     end;
  2914. num_glb_strs := 0;
  2915. @* Style-file commands.
  2916. @^style-file commands@>
  2917. There are ten \.{.bst} commands: Five (\.{entry}, \.{function},
  2918. \.{integers}, \.{macro}, and \.{strings}) declare and define
  2919. functions, one (\.{read}) reads in the \.{.bib}-file entries, and four
  2920. (\.{execute}, \.{iterate}, \.{reverse}, and \.{sort})
  2921. manipulate the entries and produce output.
  2922. The boolean variables |entry_seen| and |read_seen| indicate whether
  2923. we've yet encountered an \.{entry} and a \.{read} command.  There must
  2924. be exactly one of each of these, and the \.{entry} command, as well as
  2925. any \.{macro} command, must precede the \.{read} command.
  2926. Furthermore, the \.{read} command must precede the four that
  2927. manipulate the entries and produce output.
  2928. @<Globals in the outer block@>=
  2929. @!entry_seen : boolean;    {|true| if we've already seen an \.{entry} command}
  2930. @!read_seen : boolean;    {|true| if we've already seen a \.{read} command}
  2931. @!read_performed : boolean; {|true| if we started reading the database file(s)}
  2932. @!reading_completed : boolean; {|true| if we made it all the way through}
  2933. @!read_completed : boolean; {|true| if the database info didn't bomb \BibTeX}
  2934. And we initialize them.
  2935. @<Set initial values of key variables@>=
  2936. entry_seen := false;
  2937. read_seen := false;
  2938. read_performed := false;
  2939. reading_completed := false;
  2940. read_completed := false;
  2941. @:this can't happen}{\quad Identifier scanning error@>
  2942. Here's another bug.
  2943. @<Procedures and functions for all file I/O, error messages, and such@>=
  2944. procedure id_scanning_confusion;
  2945. begin
  2946. confusion ('Identifier scanning error');
  2947. @:this can't happen}{\quad Identifier scanning error@>
  2948. This macro is used to scan all \.{.bst} identifiers.  The argument
  2949. supplies the \.{.bst} command name.  The associated procedure simply
  2950. prints an error message.
  2951. @d bst_identifier_scan(#) ==
  2952.     begin
  2953.     scan_identifier (right_brace,comment,comment);
  2954.     if ((scan_result = white_adjacent) or
  2955.                 (scan_result = specified_char_adjacent)) then
  2956.         do_nothing
  2957.     else
  2958.         begin
  2959.         bst_id_print;
  2960.         bst_err (#);
  2961.         end;
  2962. @<Procedures and functions for all file I/O, error messages, and such@>=
  2963. procedure bst_id_print;
  2964. begin
  2965. if (scan_result = id_null) then
  2966.     print ('"',xchr[scan_char],'" begins identifier, command: ')
  2967. else if (scan_result = other_char_adjacent) then
  2968.     print ('"',xchr[scan_char],'" immediately follows identifier, command: ')
  2969.     id_scanning_confusion;
  2970. This macro just makes sure we're at a |left_brace|.
  2971. @d bst_get_and_check_left_brace(#) ==
  2972.     begin
  2973.     if (scan_char <> left_brace) then
  2974.         begin
  2975.         bst_left_brace_print;
  2976.         bst_err (#);
  2977.         end;
  2978.     incr(buf_ptr2);            {skip over the |left_brace|}
  2979. @<Procedures and functions for all file I/O, error messages, and such@>=
  2980. procedure bst_left_brace_print;
  2981. begin
  2982. print ('"',xchr[left_brace],'" is missing in command: ');
  2983. And this one, a |right_brace|.
  2984. @d bst_get_and_check_right_brace(#) ==
  2985.     begin
  2986.     if (scan_char <> right_brace) then
  2987.         begin
  2988.         bst_right_brace_print;
  2989.         bst_err (#);
  2990.         end;
  2991.     incr(buf_ptr2);            {skip over the |right_brace|}
  2992. @<Procedures and functions for all file I/O, error messages, and such@>=
  2993. procedure bst_right_brace_print;
  2994. begin
  2995. print ('"',xchr[right_brace],'" is missing in command: ');
  2996. This macro complains if we've already encountered a function to be
  2997. inserted into the hash table.
  2998. @d check_for_already_seen_function(#) ==
  2999.     begin
  3000.     if (hash_found) then  {already encountered this as a \.{.bst} function}
  3001.         begin
  3002.         already_seen_function_print (#);
  3003.         return;
  3004.         end;
  3005. @<Procedures and functions for all file I/O, error messages, and such@>=
  3006. procedure already_seen_function_print (@!seen_fn_loc : hash_loc);
  3007. label exit;    {so the call to |bst_err| works}
  3008. begin
  3009. print_pool_str (hash_text[seen_fn_loc]);
  3010. print (' is already a type "');
  3011. print_fn_class (seen_fn_loc);
  3012. print_ln ('" function name');
  3013. bst_err_print_and_look_for_blank_line_return;
  3014. exit:
  3015. @:style-file commands}{\quad \.{entry}@>
  3016. An \.{entry} command has three arguments, each a (possibly empty) list
  3017. of function names between braces (the names are separated by one or
  3018. more |white_space| characters).  All function names in this and other
  3019. commands must be legal \.{.bst} identifiers.  Upper/lower cases are
  3020. considered to be the same for function names in these lists---all
  3021. upper-case letters are converted to lower case.  These arguments give
  3022. lists of |field|s, |int_entry_var|s, and |str_entry_var|s.
  3023. @<Procedures and functions for the reading and processing of input files@>=
  3024. procedure bst_entry_command;
  3025. label exit;
  3026. begin
  3027. if (entry_seen) then
  3028.     bst_err ('Illegal, another entry command');
  3029. entry_seen := true;        {now we've seen an \.{entry} command}
  3030. eat_bst_white_and_eof_check ('entry');
  3031. @<Scan the list of |field|s@>;
  3032. eat_bst_white_and_eof_check ('entry');
  3033. if (num_fields = num_pre_defined_fields) then
  3034.     bst_warn ('Warning--I didn''t find any fields');
  3035. @<Scan the list of |int_entry_var|s@>;
  3036. eat_bst_white_and_eof_check ('entry');
  3037. @<Scan the list of |str_entry_var|s@>;
  3038. exit:
  3039. This module reads a |left_brace|, the list of |field|s, and a
  3040. |right_brace|.  The |field|s are those like `author' and `title.'
  3041. @<Scan the list of |field|s@>=
  3042. begin
  3043. bst_get_and_check_left_brace ('entry');
  3044. eat_bst_white_and_eof_check ('entry');
  3045. while (scan_char <> right_brace) do
  3046.     begin
  3047.     bst_identifier_scan ('entry');
  3048.     @<Insert a |field| into the hash table@>;
  3049.     eat_bst_white_and_eof_check ('entry');
  3050.     end;
  3051. incr(buf_ptr2);            {skip over the |right_brace|}
  3052. @^secret agent man@>
  3053. Here we insert the just found field name into the hash table, record
  3054. it as a |field|, and assign it a number to be used in indexing into
  3055. the |field_info| array.
  3056. @<Insert a |field| into the hash table@>=
  3057. begin
  3058.   trace
  3059.   trace_pr_token;
  3060.   trace_pr_ln (' is a field');
  3061.   ecart@/
  3062. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3063. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3064. check_for_already_seen_function (fn_loc);
  3065. fn_type[fn_loc] := field;@/
  3066. fn_info[fn_loc] := num_fields;    {give this field a number (take away its name)}
  3067. incr(num_fields);
  3068. This module reads a |left_brace|, the list of |int_entry_var|s,
  3069. and a |right_brace|.
  3070. @<Scan the list of |int_entry_var|s@>=
  3071. begin
  3072. bst_get_and_check_left_brace ('entry');
  3073. eat_bst_white_and_eof_check ('entry');
  3074. while (scan_char <> right_brace) do
  3075.     begin
  3076.     bst_identifier_scan ('entry');
  3077.     @<Insert an |int_entry_var| into the hash table@>;
  3078.     eat_bst_white_and_eof_check ('entry');
  3079.     end;
  3080. incr(buf_ptr2);            {skip over the |right_brace|}
  3081. Here we insert the just found |int_entry_var| name into the hash table
  3082. and record it as an |int_entry_var|.  An |int_entry_var| is one that
  3083. the style designer wants a separate copy of for each entry.
  3084. @<Insert an |int_entry_var| into the hash table@>=
  3085. begin
  3086.   trace
  3087.   trace_pr_token;
  3088.   trace_pr_ln (' is an integer entry-variable');
  3089.   ecart@/
  3090. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3091. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3092. check_for_already_seen_function (fn_loc);
  3093. fn_type[fn_loc] := int_entry_var;@/
  3094. fn_info[fn_loc] := num_ent_ints;    {give this |int_entry_var| a number}
  3095. incr(num_ent_ints);
  3096. This module reads a |left_brace|, the list of |str_entry_var|s, and a
  3097. |right_brace|.  A |str_entry_var| is one that the style designer wants
  3098. a separate copy of for each entry.
  3099. @<Scan the list of |str_entry_var|s@>=
  3100. begin
  3101. bst_get_and_check_left_brace ('entry');
  3102. eat_bst_white_and_eof_check ('entry');
  3103. while (scan_char <> right_brace) do
  3104.     begin
  3105.     bst_identifier_scan ('entry');
  3106.     @<Insert a |str_entry_var| into the hash table@>;
  3107.     eat_bst_white_and_eof_check ('entry');
  3108.     end;
  3109. incr(buf_ptr2);            {skip over the |right_brace|}
  3110. Here we insert the just found |str_entry_var| name into the hash
  3111. table, record it as a |str_entry_var|, and set its pointer into
  3112. |entry_strs|.
  3113. @<Insert a |str_entry_var| into the hash table@>=
  3114. begin
  3115.   trace
  3116.   trace_pr_token;
  3117.   trace_pr_ln (' is a string entry-variable');
  3118.   ecart@/
  3119. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3120. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3121. check_for_already_seen_function (fn_loc);
  3122. fn_type[fn_loc] := str_entry_var;@/
  3123. fn_info[fn_loc] := num_ent_strs;    {give this |str_entry_var| a number}
  3124. incr(num_ent_strs);
  3125. A legal argument for an \.{execute}, \.{iterate}, or \.{reverse}
  3126. command must exist and be |built_in| or |wiz_defined|.
  3127. Here's where we check, returning |true| if the argument is illegal.
  3128. @<Procedures and functions for the reading and processing of input files@>=
  3129. function bad_argument_token : boolean;
  3130. label exit;
  3131. begin
  3132. bad_argument_token := true;    {now it's easy to exit if necessary}
  3133. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3134. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  3135. if (not hash_found) then            {unknown \.{.bst} function}
  3136.     begin
  3137.     print_token;
  3138.     bst_err (' is an unknown function');
  3139.     end
  3140. else if ((fn_type[fn_loc] <> built_in) and
  3141.      (fn_type[fn_loc] <> wiz_defined)) then
  3142.     begin
  3143.     print_token;
  3144.     print (' has bad function type ');
  3145.     print_fn_class (fn_loc);
  3146.     bst_err_print_and_look_for_blank_line_return;
  3147.     end;
  3148. bad_argument_token := false;
  3149. exit:
  3150. @:style-file commands}{\quad \.{execute}@>
  3151. An \.{execute} command has one argument, a single |built_in| or
  3152. |wiz_defined| function name between braces.  Upper/lower cases are
  3153. considered to be the same---all upper-case letters are converted to
  3154. lower case.  Also, we must make sure we've already seen a \.{read}
  3155. command.
  3156. This module reads a |left_brace|, a single function to be executed,
  3157. and a |right_brace|.
  3158. @<Procedures and functions for the reading and processing of input files@>=
  3159. procedure bst_execute_command;
  3160. label exit;
  3161. begin
  3162. if (not read_seen) then
  3163.     bst_err ('Illegal, execute command before read command');
  3164. eat_bst_white_and_eof_check ('execute');
  3165. bst_get_and_check_left_brace ('execute');
  3166. eat_bst_white_and_eof_check ('execute');
  3167. bst_identifier_scan ('execute');
  3168. @<Check the \.{execute}-command argument token@>;
  3169. eat_bst_white_and_eof_check ('execute');
  3170. bst_get_and_check_right_brace ('execute');
  3171. @<Perform an \.{execute} command@>;
  3172. exit:
  3173. Before executing the function, we must make sure it's a legal one.  It
  3174. must exist and be |built_in| or |wiz_defined|.
  3175. @<Check the \.{execute}-command argument token@>=
  3176. begin
  3177.   trace
  3178.   trace_pr_token;
  3179.   trace_pr_ln (' is a to be executed function');
  3180.   ecart@/
  3181. if (bad_argument_token) then
  3182.     return;
  3183. @:style-file commands}{\quad \.{function}@>
  3184. A \.{function} command has two arguments; the first is a
  3185. |wiz_defined| function name between braces.  Upper/lower cases are
  3186. considered to be the same---all upper-case letters are converted to
  3187. lower case.  The second argument defines this function.  It consists
  3188. of a sequence of functions, between braces, separated by |white_space|
  3189. characters.  Upper/lower cases are considered to be the same for
  3190. function names but not for |str_literal|s.
  3191. @<Procedures and functions for the reading and processing of input files@>=
  3192. procedure bst_function_command;
  3193. label exit;
  3194. begin
  3195. eat_bst_white_and_eof_check ('function');
  3196. @<Scan the |wiz_defined| function name@>;
  3197. eat_bst_white_and_eof_check ('function');
  3198. bst_get_and_check_left_brace ('function');
  3199. scan_fn_def(wiz_loc);        {this scans the function definition}
  3200. exit:
  3201. This module reads a |left_brace|, a |wiz_defined| function name, and
  3202. a |right_brace|.
  3203. @<Scan the |wiz_defined| function name@>=
  3204. begin
  3205. bst_get_and_check_left_brace ('function');
  3206. eat_bst_white_and_eof_check ('function');
  3207. bst_identifier_scan ('function');
  3208. @<Check the |wiz_defined| function name@>;
  3209. eat_bst_white_and_eof_check ('function');
  3210. bst_get_and_check_right_brace ('function');
  3211. The function name must exist and be a new one; we mark it as
  3212. |wiz_defined|.  Also, see if it's the default entry-type function.
  3213. @<Check the |wiz_defined| function name@>=
  3214. begin
  3215.   trace
  3216.   trace_pr_token;
  3217.   trace_pr_ln (' is a wizard-defined function');
  3218.   ecart@/
  3219. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3220. wiz_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3221. check_for_already_seen_function (wiz_loc);
  3222. fn_type[wiz_loc] := wiz_defined;
  3223. if (hash_text[wiz_loc] = s_default) then  {we've found the default entry-type}
  3224.     b_default := wiz_loc;    {see the |built_in| functions for |b_default|}
  3225. We're about to start scanning tokens in a function definition.  When a
  3226. function token is illegal, we skip until it ends; a |white_space|
  3227. character, an end-of-line, a |right_brace|, or a |comment| marks the
  3228. end of the current token.
  3229. @d next_token=25        {a bad function token; go read the next one}
  3230. @d skip_token(#) == begin    {not-so-serious error during \.{.bst} parsing}
  3231.             print (#);
  3232.             skip_token_print;    {also, skip to the current token's end}
  3233.             goto next_token;
  3234.             end
  3235. @<Procedures and functions for input scanning@>=
  3236. procedure skip_token_print;
  3237. begin
  3238. print ('-');
  3239. bst_ln_num_print;
  3240. mark_error;
  3241. if (scan2_white(right_brace,comment)) then          {ok if token ends line}
  3242.     do_nothing;
  3243. @^commented-out code@>
  3244. @^for a good time, try comment-out code@>
  3245. This macro is similar to the last one but is specifically for
  3246. recursion in a |wiz_defined| function, which is illegal; it helps save
  3247. space.
  3248. @d skip_recursive_token == begin
  3249.                print_recursion_illegal;
  3250.                goto next_token;
  3251.                end
  3252. @<Procedures and functions for input scanning@>=
  3253. procedure print_recursion_illegal;
  3254. begin
  3255.   trace
  3256.   trace_pr_newline;
  3257.   ecart@/
  3258. print_ln ('Curse you, wizard, before you recurse me:');
  3259. print ('function ');
  3260. print_token;
  3261. print_ln (' is illegal in its own definition');
  3262.   print_recursion_illegal;
  3263.   @}@/
  3264. skip_token_print;            {also, skip to the current token's end}
  3265. Here's another macro for saving some space when there's a problem with
  3266. a token.
  3267. @d skip_token_unknown_function == begin
  3268.                   skp_token_unknown_function_print;
  3269.                   goto next_token;
  3270.                   end
  3271. @<Procedures and functions for input scanning@>=
  3272. procedure skp_token_unknown_function_print;
  3273. begin
  3274. print_token;
  3275. print (' is an unknown function');
  3276. skip_token_print;            {also, skip to the current token's end}
  3277. And another.
  3278. @d skip_token_illegal_stuff_after_literal ==
  3279.             begin
  3280.             skip_illegal_stuff_after_token_print;
  3281.             goto next_token;
  3282.             end
  3283. @<Procedures and functions for input scanning@>=
  3284. procedure skip_illegal_stuff_after_token_print;
  3285. begin
  3286. print ('"',xchr[scan_char],'" can''t follow a literal');
  3287. skip_token_print;            {also, skip to the current token's end}
  3288. This recursive function reads and stores the list of functions
  3289. (separated by |white_space| characters or ends-of-line) that define
  3290. this new function, and reads a |right_brace|.
  3291. @<Procedures and functions for input scanning@>=
  3292. procedure scan_fn_def (@!fn_hash_loc : hash_loc);
  3293. label next_token,@!exit;
  3294. type @!fn_def_loc = 0..single_fn_space;    {for a single |wiz_defined|-function}
  3295. var singl_function : packed array[fn_def_loc] of hash_ptr2;
  3296.     @!single_ptr : fn_def_loc;    {next storage location for this definition}
  3297.     @!copy_ptr : fn_def_loc;    {dummy variable}
  3298.     @!end_of_num : buf_pointer;    {the end of an implicit function's name}
  3299.     @!impl_fn_loc : hash_loc;    {an implicit function's hash-table location}
  3300. begin
  3301. eat_bst_white_and_eof_check ('function');
  3302. single_ptr := 0;
  3303. while (scan_char <> right_brace) do
  3304.     begin
  3305.     @<Get the next function of the definition@>;
  3306. next_token:
  3307.     eat_bst_white_and_eof_check ('function');
  3308.     end;
  3309. @<Complete this function's definition@>;
  3310. incr(buf_ptr2);            {skip over the |right_brace|}
  3311. exit:
  3312. @:BibTeX capacity exceeded}{\quad single function space@>
  3313. This macro inserts a hash-table location (or one of the two
  3314. special markers |quote_next_fn| and |end_of_def|) into the
  3315. |singl_function| array, which will later be copied into the
  3316. |wiz_functions| array.
  3317. @d insert_fn_loc(#) ==    begin
  3318.             singl_function[single_ptr] := #;
  3319.             if (single_ptr = single_fn_space) then
  3320.                 singl_fn_overflow;
  3321.             incr(single_ptr);
  3322.             end
  3323. @<Procedures and functions for all file I/O, error messages, and such@>=
  3324. procedure singl_fn_overflow;
  3325. begin
  3326. overflow('single function space ',single_fn_space);
  3327. There are five possibilities for the first character of the token
  3328. representing the next function of the definition: If it's a
  3329. |number_sign|, the token is an |int_literal|; if it's a
  3330. |double_quote|, the token is a |str_literal|; if it's a
  3331. |single_quote|, the token is a quoted function; if it's a
  3332. |left_brace|, the token isn't really a token, but rather the start of
  3333. another function definition (which will result in a recursive call to
  3334. |scan_fn_def|); if it's anything else, the token is the name of an
  3335. already-defined function.  Note: To prevent the wizard from using
  3336. recursion, we have to check that neither a quoted function nor an
  3337. already-defined-function is actually the currently-being-defined
  3338. function (which is stored at |wiz_loc|).
  3339. @<Get the next function of the definition@>=
  3340. case (scan_char) of
  3341.     number_sign : @<Scan an |int_literal|@>;
  3342.     double_quote : @<Scan a |str_literal|@>;
  3343.     single_quote : @<Scan a quoted function@>;
  3344.     left_brace : @<Start a new function definition@>;
  3345.     othercases @<Scan an already-defined function@>
  3346. endcases
  3347. An |int_literal| is preceded by a |number_sign|, consists of an
  3348. integer (i.e., an optional |minus_sign| followed by one or more
  3349. |numeric| characters), and is followed either by a |white_space|
  3350. character, an end-of-line, or a |right_brace|.  The array |fn_info|
  3351. contains the value of the integer for |int_literal|s.
  3352. @<Scan an |int_literal|@>=
  3353. begin
  3354. incr(buf_ptr2);                {skip over the |number_sign|}
  3355. if (not scan_integer) then
  3356.     skip_token ('Illegal integer in integer literal');
  3357.   trace
  3358.   trace_pr ('#');
  3359.   trace_pr_token;
  3360.   trace_pr_ln (' is an integer literal with value ',token_value:0);
  3361.   ecart@/
  3362. literal_loc := str_lookup(buffer,buf_ptr1,token_len,integer_ilk,do_insert);
  3363. if (not hash_found) then
  3364.     begin
  3365.     fn_type[literal_loc] := int_literal;    {set the |fn_class|}
  3366.     fn_info[literal_loc] := token_value;    {the value of this integer}
  3367.     end;
  3368. if ((lex_class[scan_char]<>white_space) and (buf_ptr2<last) and
  3369.         (scan_char<>right_brace) and@| (scan_char<>comment)) then
  3370.     skip_token_illegal_stuff_after_literal;
  3371. insert_fn_loc (literal_loc);    {add this function to |wiz_functions|}
  3372. A |str_literal| is preceded by a |double_quote| and consists of all
  3373. characters on this line up to the next |double_quote|.  Also, there
  3374. must be either a |white_space| character, an end-of-line, a
  3375. |right_brace|, or a |comment| following (since functions in the
  3376. definition must be separated by |white_space|).  The array |fn_info|
  3377. contains nothing for |str_literal|s.
  3378. @<Scan a |str_literal|@>=
  3379. begin
  3380. incr(buf_ptr2);                {skip over the |double_quote|}
  3381. if (not scan1(double_quote)) then
  3382.     skip_token ('No `',xchr[double_quote],''' to end string literal');
  3383.   trace
  3384.   trace_pr ('"');
  3385.   trace_pr_token;
  3386.   trace_pr ('"');
  3387.   trace_pr_ln (' is a string literal');
  3388.   ecart@/
  3389. literal_loc := str_lookup(buffer,buf_ptr1,token_len,text_ilk,do_insert);@/
  3390. fn_type[literal_loc] := str_literal;    {set the |fn_class|}
  3391. incr(buf_ptr2);                {skip over the |double_quote|}
  3392. if ((lex_class[scan_char]<>white_space) and (buf_ptr2<last) and
  3393.     (scan_char<>right_brace) and@| (scan_char<>comment)) then
  3394.     skip_token_illegal_stuff_after_literal;
  3395. insert_fn_loc (literal_loc);        {add this function to |wiz_functions|}
  3396. A quoted function is preceded by a |single_quote| and consists of all
  3397. characters up to the next |white_space| character, end-of-line,
  3398. |right_brace|, or |comment|.
  3399. @<Scan a quoted function@>=
  3400. begin
  3401. incr(buf_ptr2);                    {skip over the |single_quote|}
  3402. if (scan2_white(right_brace,comment)) then          {ok if token ends line}
  3403.     do_nothing;
  3404.   trace
  3405.   trace_pr ('''');
  3406.   trace_pr_token;
  3407.   trace_pr (' is a quoted function ');
  3408.   ecart@/
  3409. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3410. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  3411. if (not hash_found) then            {unknown \.{.bst} function}
  3412.     skip_token_unknown_function
  3413.     @<Check and insert the quoted function@>;
  3414. Here we check that this quoted function is a legal one---the function
  3415. name must already exist, but it mustn't be the currently-being-defined
  3416. function (which is stored at |wiz_loc|).
  3417. @<Check and insert the quoted function@>=
  3418. begin
  3419. if (fn_loc = wiz_loc) then
  3420.     skip_recursive_token
  3421.     begin
  3422.       trace
  3423.       trace_pr ('of type ');
  3424.       trace_pr_fn_class (fn_loc);
  3425.       trace_pr_newline;
  3426.       ecart@/
  3427.     insert_fn_loc (quote_next_fn);    {add special marker together with}
  3428.     insert_fn_loc (fn_loc);        {this function to |wiz_functions|}
  3429.     end
  3430. @^kludge@>
  3431. @:this can't happen}{\quad Already encountered implicit function@>
  3432. This module marks the implicit function as being quoted, generates a
  3433. name, and stores it in the hash table.  This name is strictly internal
  3434. to this program, starts with a |single_quote| (since that will make
  3435. this function name unique), and ends with the variable |impl_fn_num|
  3436. converted to ASCII.  The alias kludge helps make the stack space not
  3437. overflow on some machines.
  3438. @d ex_buf2 == ex_buf        {an alias, used only in this module}
  3439. @<Start a new function definition@>=
  3440. begin
  3441. ex_buf2[0] := single_quote;
  3442. int_to_ASCII (impl_fn_num,ex_buf2,1,end_of_num);
  3443. impl_fn_loc := str_lookup(ex_buf2,0,end_of_num,bst_fn_ilk,do_insert);
  3444. if (hash_found) then
  3445.     confusion ('Already encountered implicit function');
  3446.   trace
  3447.   trace_pr_pool_str (hash_text[impl_fn_loc]);
  3448.   trace_pr_ln (' is an implicit function');
  3449.   ecart@/
  3450. incr(impl_fn_num);
  3451. fn_type[impl_fn_loc] := wiz_defined;@/
  3452. insert_fn_loc (quote_next_fn);    {all implicit functions are quoted}
  3453. insert_fn_loc (impl_fn_loc);    {add it to |wiz_functions|}
  3454. incr(buf_ptr2);            {skip over the |left_brace|}
  3455. scan_fn_def (impl_fn_loc);    {this is the recursive call}
  3456. The variable |impl_fn_num| counts the number of implicit functions
  3457. seen in the \.{.bst} file.
  3458. @<Globals in the outer block@>=
  3459. @!impl_fn_num : integer;    {the number of implicit functions seen so far}
  3460. Now we initialize it.
  3461. @<Set initial values of key variables@>=
  3462. impl_fn_num := 0;
  3463. @:BibTeX capacity exceeded}{\quad buffer size@>
  3464. This module appends a character to |int_buf| after checking to make
  3465. sure it will fit; for use in |int_to_ASCII|.
  3466. @d append_int_char(#) == begin
  3467.              if (int_ptr = buf_size) then
  3468.                  buffer_overflow;
  3469.              int_buf[int_ptr]:=#;
  3470.              incr(int_ptr);
  3471.              end
  3472. This procedure takes the integer |int|, copies the appropriate
  3473. |ASCII_code| string into |int_buf| starting at |int_begin|, and sets
  3474. the |var| parameter |int_end| to the first unused |int_buf| location.
  3475. The ASCII string will consist of decimal digits, the first of which
  3476. will be not be a~0 if the integer is nonzero, with a prepended minus
  3477. sign if the integer is negative.
  3478. @<Procedures and functions for handling numbers, characters, and strings@>=
  3479. procedure int_to_ASCII (@!int:integer; var int_buf:buf_type;
  3480.             @!int_begin:buf_pointer; var int_end:buf_pointer);
  3481. var int_ptr,@!int_xptr : buf_pointer;    {pointers into |int_buf|}
  3482.   @!int_tmp_val : ASCII_code;        {the temporary element in an exchange}
  3483. begin
  3484. int_ptr := int_begin;
  3485. if (int < 0) then    {add the |minus_sign| and use the absolute value}
  3486.     begin
  3487.     append_int_char (minus_sign);
  3488.     int := -int;
  3489.     end;
  3490. int_xptr := int_ptr;
  3491. repeat                {copy digits into |int_buf|}
  3492.     append_int_char ("0" + (int mod 10));
  3493.     int := int div 10;
  3494.   until (int = 0);
  3495. int_end := int_ptr;        {set the string length}
  3496. decr(int_ptr);
  3497. while (int_xptr < int_ptr) do    {and reorder (flip) the digits}
  3498.     begin
  3499.     int_tmp_val := int_buf[int_xptr];
  3500.     int_buf[int_xptr] := int_buf[int_ptr];
  3501.     int_buf[int_ptr] := int_tmp_val;
  3502.     decr(int_ptr);
  3503.     incr(int_xptr);
  3504.     end
  3505. An already-defined function consists of all characters up to the next
  3506. |white_space| character, end-of-line, |right_brace|, or |comment|.
  3507. This function name must already exist, but it mustn't be the
  3508. currently-being-defined function (which is stored at |wiz_loc|).
  3509. @<Scan an already-defined function@>=
  3510. begin
  3511. if (scan2_white(right_brace,comment)) then          {ok if token ends line}
  3512.     do_nothing;
  3513.   trace
  3514.   trace_pr_token;
  3515.   trace_pr (' is a function ');
  3516.   ecart@/
  3517. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3518. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  3519. if (not hash_found) then            {unknown \.{.bst} function}
  3520.     skip_token_unknown_function
  3521. else if (fn_loc = wiz_loc) then
  3522.     skip_recursive_token
  3523.     begin
  3524.       trace
  3525.       trace_pr ('of type ');
  3526.       trace_pr_fn_class (fn_loc);
  3527.       trace_pr_newline;
  3528.       ecart@/
  3529.     insert_fn_loc (fn_loc);    {add this function to |wiz_functions|}
  3530.     end;
  3531. @:BibTeX capacity exceeded}{\quad wizard-defined function space@>
  3532. Now we add the |end_of_def| special marker, make sure this function will
  3533. fit into |wiz_functions|, and put it there.
  3534. @<Complete this function's definition@>=
  3535. begin
  3536. insert_fn_loc (end_of_def);  {add special marker ending the definition}
  3537. if (single_ptr + wiz_def_ptr > wiz_fn_space) then
  3538.     begin
  3539.     print (single_ptr + wiz_def_ptr : 0,': ');
  3540.     overflow('wizard-defined function space ',wiz_fn_space);
  3541.     end;
  3542. fn_info[fn_hash_loc] := wiz_def_ptr;        {pointer into |wiz_functions|}
  3543. copy_ptr := 0;
  3544. while (copy_ptr < single_ptr) do        {make this function official}
  3545.     begin
  3546.     wiz_functions[wiz_def_ptr] := singl_function[copy_ptr];
  3547.     incr(copy_ptr);
  3548.     incr(wiz_def_ptr);
  3549.     end;
  3550. @:style-file commands}{\quad \.{integers}@>
  3551. An \.{integers} command has one argument, a list of function names
  3552. between braces (the names are separated by one or more |white_space|
  3553. characters).  Upper/lower cases are considered to be the same for
  3554. function names in these lists---all upper-case letters are converted to
  3555. lower case.  Each name in this list specifies an |int_global_var|.
  3556. There may be several \.{integers} commands in the \.{.bst} file.
  3557. This module reads a |left_brace|, a list of |int_global_var|s, and a
  3558. |right_brace|.
  3559. @<Procedures and functions for the reading and processing of input files@>=
  3560. procedure bst_integers_command;
  3561. label exit;
  3562. begin
  3563. eat_bst_white_and_eof_check ('integers');
  3564. bst_get_and_check_left_brace ('integers');
  3565. eat_bst_white_and_eof_check ('integers');
  3566. while (scan_char <> right_brace) do
  3567.     begin
  3568.     bst_identifier_scan ('integers');
  3569.     @<Insert an |int_global_var| into the hash table@>;
  3570.     eat_bst_white_and_eof_check ('integers');
  3571.     end;
  3572. incr(buf_ptr2);            {skip over the |right_brace|}
  3573. exit:
  3574. Here we insert the just found |int_global_var| name into the hash
  3575. table and record it as an |int_global_var|.  Also, we initialize it by
  3576. setting |fn_info[fn_loc]| to 0.
  3577. @<Insert an |int_global_var| into the hash table@>=
  3578. begin
  3579.   trace
  3580.   trace_pr_token;
  3581.   trace_pr_ln (' is an integer global-variable');
  3582.   ecart@/
  3583. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3584. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3585. check_for_already_seen_function (fn_loc);
  3586. fn_type[fn_loc] := int_global_var;@/
  3587. fn_info[fn_loc] := 0;                {initialize}
  3588. @:style-file commands}{\quad \.{iterate}@>
  3589. An \.{iterate} command has one argument, a single |built_in| or
  3590. |wiz_defined| function name between braces.  Upper/lower cases are
  3591. considered to be the same---all upper-case letters are converted to
  3592. lower case.  Also, we must make sure we've already seen a \.{read}
  3593. command.
  3594. This module reads a |left_brace|, a single function to be iterated,
  3595. and a |right_brace|.
  3596. @<Procedures and functions for the reading and processing of input files@>=
  3597. procedure bst_iterate_command;
  3598. label exit;
  3599. begin
  3600. if (not read_seen) then
  3601.     bst_err ('Illegal, iterate command before read command');
  3602. eat_bst_white_and_eof_check ('iterate');
  3603. bst_get_and_check_left_brace ('iterate');
  3604. eat_bst_white_and_eof_check ('iterate');
  3605. bst_identifier_scan ('iterate');
  3606. @<Check the \.{iterate}-command argument token@>;
  3607. eat_bst_white_and_eof_check ('iterate');
  3608. bst_get_and_check_right_brace ('iterate');
  3609. @<Perform an \.{iterate} command@>;
  3610. exit:
  3611. Before iterating the function, we must make sure it's a legal one.  It
  3612. must exist and be |built_in| or |wiz_defined|.
  3613. @<Check the \.{iterate}-command argument token@>=
  3614. begin
  3615.   trace
  3616.   trace_pr_token;
  3617.   trace_pr_ln (' is a to be iterated function');
  3618.   ecart@/
  3619. if (bad_argument_token) then
  3620.     return;
  3621. @:style-file commands}{\quad \.{macro}@>
  3622. A \.{macro} command, like a \.{function} command, has two arguments;
  3623. the first is a macro name between braces.  The name must be a legal
  3624. \.{.bst} identifier.  Upper/lower cases are considered to be the
  3625. same---all upper-case letters are converted to lower case.  The second
  3626. argument defines this macro.  It consists of a
  3627. |double_quote|-delimited string (which must be on a single line)
  3628. between braces, with optional |white_space| characters between the
  3629. braces and the |double_quote|s.  This |double_quote|-delimited string
  3630. is parsed exactly as a |str_literal| is for the \.{function} command.
  3631. @<Procedures and functions for the reading and processing of input files@>=
  3632. procedure bst_macro_command;
  3633. label exit;
  3634. begin
  3635. if (read_seen) then
  3636.     bst_err ('Illegal, macro command after read command');
  3637. eat_bst_white_and_eof_check ('macro');
  3638. @<Scan the macro name@>;
  3639. eat_bst_white_and_eof_check ('macro');
  3640. @<Scan the macro's definition@>;
  3641. exit:
  3642. This module reads a |left_brace|, a macro name, and a |right_brace|.
  3643. @<Scan the macro name@>=
  3644. begin
  3645. bst_get_and_check_left_brace ('macro');
  3646. eat_bst_white_and_eof_check ('macro');
  3647. bst_identifier_scan ('macro');
  3648. @<Check the macro name@>;
  3649. eat_bst_white_and_eof_check ('macro');
  3650. bst_get_and_check_right_brace ('macro');
  3651. The macro name must be a new one; we mark it as |macro_ilk|.
  3652. @<Check the macro name@>=
  3653. begin
  3654.   trace
  3655.   trace_pr_token;
  3656.   trace_pr_ln (' is a macro');
  3657.   ecart@/
  3658. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3659. macro_name_loc := str_lookup(buffer,buf_ptr1,token_len,macro_ilk,do_insert);
  3660. if (hash_found) then
  3661.     begin
  3662.     print_token;
  3663.     bst_err (' is already defined as a macro');
  3664.     end;
  3665. ilk_info[macro_name_loc]:=hash_text[macro_name_loc]; {default in case of error}
  3666. This module reads a |left_brace|, the |double_quote|-delimited string
  3667. that defines this macro, and a |right_brace|.
  3668. @<Scan the macro's definition@>=
  3669. begin
  3670. bst_get_and_check_left_brace ('macro');
  3671. eat_bst_white_and_eof_check ('macro');
  3672. if (scan_char <> double_quote) then
  3673.     bst_err ('A macro definition must be ',xchr[double_quote],'-delimited');
  3674. @<Scan the macro definition-string@>;
  3675. eat_bst_white_and_eof_check ('macro');
  3676. bst_get_and_check_right_brace ('macro');
  3677. A macro definition-string is preceded by a |double_quote| and consists
  3678. of all characters on this line up to the next |double_quote|.  The
  3679. array |ilk_info| contains a pointer to this string for the macro name.
  3680. @<Scan the macro definition-string@>=
  3681. begin
  3682. incr(buf_ptr2);                {skip over the |double_quote|}
  3683. if (not scan1(double_quote)) then
  3684.     bst_err ('There''s no `',xchr[double_quote],''' to end macro definition');
  3685.   trace
  3686.   trace_pr ('"');
  3687.   trace_pr_token;
  3688.   trace_pr ('"');
  3689.   trace_pr_ln (' is a macro string');
  3690.   ecart@/
  3691. macro_def_loc := str_lookup(buffer,buf_ptr1,token_len,text_ilk,do_insert);@/
  3692. fn_type[macro_def_loc] := str_literal;    {set the |fn_class|}
  3693. ilk_info[macro_name_loc] := hash_text[macro_def_loc];
  3694. incr(buf_ptr2);                {skip over the |double_quote|}
  3695. @^gymnastics@>
  3696. We need to include stuff for \.{.bib} reading here because that's done
  3697. by the \.{read} command.
  3698. @<Procedures and functions for the reading and processing of input files@>=
  3699. @<Scan for and process a \.{.bib} command or database entry@>
  3700. @:style-file commands}{\quad \.{read}@>
  3701. The \.{read} command has no arguments so there's no more parsing to
  3702. do.  We must make sure we haven't seen a \.{read} command before and
  3703. we've already seen an \.{entry} command.
  3704. @<Procedures and functions for the reading and processing of input files@>=
  3705. procedure bst_read_command;
  3706. label exit;
  3707. begin
  3708. if (read_seen) then
  3709.     bst_err ('Illegal, another read command');
  3710. read_seen := true;        {now we've seen a \.{read} command}
  3711. if (not entry_seen) then
  3712.     bst_err ('Illegal, read command before entry command');
  3713. sv_ptr1 := buf_ptr2;        {save the contents of the \.{.bst} input line}
  3714. sv_ptr2 := last;
  3715. tmp_ptr := sv_ptr1;
  3716. while (tmp_ptr < sv_ptr2) do
  3717.     begin
  3718.     sv_buffer[tmp_ptr] := buffer[tmp_ptr];
  3719.     incr(tmp_ptr);
  3720.     end;
  3721. @<Read the \.{.bib} file(s)@>;
  3722. buf_ptr2 := sv_ptr1;        {and restore}
  3723. last := sv_ptr2;
  3724. tmp_ptr := buf_ptr2;
  3725. while (tmp_ptr < last) do
  3726.     begin
  3727.     buffer[tmp_ptr] := sv_buffer[tmp_ptr];
  3728.     incr(tmp_ptr);
  3729.     end;
  3730. exit:
  3731. @:style-file commands}{\quad \.{reverse}@>
  3732. A \.{reverse} command has one argument, a single |built_in| or
  3733. |wiz_defined| function name between braces.  Upper/lower cases are
  3734. considered to be the same---all upper-case letters are converted to
  3735. lower case.  Also, we must make sure we've already seen a \.{read}
  3736. command.
  3737. This module reads a |left_brace|, a single function to be iterated in
  3738. reverse, and a |right_brace|.
  3739. @<Procedures and functions for the reading and processing of input files@>=
  3740. procedure bst_reverse_command;
  3741. label exit;
  3742. begin
  3743. if (not read_seen) then
  3744.     bst_err ('Illegal, reverse command before read command');
  3745. eat_bst_white_and_eof_check ('reverse');
  3746. bst_get_and_check_left_brace ('reverse');
  3747. eat_bst_white_and_eof_check ('reverse');
  3748. bst_identifier_scan ('reverse');
  3749. @<Check the \.{reverse}-command argument token@>;
  3750. eat_bst_white_and_eof_check ('reverse');
  3751. bst_get_and_check_right_brace ('reverse');
  3752. @<Perform a \.{reverse} command@>;
  3753. exit:
  3754. Before iterating the function in reverse, we must make sure it's a
  3755. legal one.  It must exist and be |built_in| or |wiz_defined|.
  3756. @<Check the \.{reverse}-command argument token@>=
  3757. begin
  3758.   trace
  3759.   trace_pr_token;
  3760.   trace_pr_ln (' is a to be iterated in reverse function');
  3761.   ecart@/
  3762. if (bad_argument_token) then
  3763.     return;
  3764. @:style-file commands}{\quad \.{sort}@>
  3765. The \.{sort} command has no arguments so there's no more parsing to
  3766. do, but we must make sure we've already seen a \.{read} command.
  3767. @<Procedures and functions for the reading and processing of input files@>=
  3768. procedure bst_sort_command;
  3769. label exit;
  3770. begin
  3771. if (not read_seen) then
  3772.     bst_err ('Illegal, sort command before read command');
  3773. @<Perform a \.{sort} command@>;
  3774. exit:
  3775. @:style-file commands}{\quad \.{strings}@>
  3776. A \.{strings} command has one argument, a list of function names
  3777. between braces (the names are separated by one or more |white_space|
  3778. characters).  Upper/lower cases are considered to be the same for
  3779. function names in these lists---all upper-case letters are converted to
  3780. lower case.  Each name in this list specifies a |str_global_var|.
  3781. There may be several \.{strings} commands in the \.{.bst} file.
  3782. This module reads a |left_brace|, a list of |str_global_var|s,
  3783. and a |right_brace|.
  3784. @<Procedures and functions for the reading and processing of input files@>=
  3785. procedure bst_strings_command;
  3786. label exit;
  3787. begin
  3788. eat_bst_white_and_eof_check ('strings');
  3789. bst_get_and_check_left_brace ('strings');
  3790. eat_bst_white_and_eof_check ('strings');
  3791. while (scan_char <> right_brace) do
  3792.     begin
  3793.     bst_identifier_scan ('strings');
  3794.     @<Insert a |str_global_var| into the hash table@>;
  3795.     eat_bst_white_and_eof_check ('strings');
  3796.     end;
  3797. incr(buf_ptr2);            {skip over the |right_brace|}
  3798. exit:
  3799. @:BibTeX capacity exceeded}{\quad number of string global-variables@>
  3800. Here we insert the just found |str_global_var| name into the hash
  3801. table, record it as a |str_global_var|, set its pointer into
  3802. |global_strs|, and initialize its value there to the null string.
  3803. @d end_of_string = invalid_code     {this illegal |ASCII_code| ends a string}
  3804. @<Insert a |str_global_var| into the hash table@>=
  3805. begin
  3806.   trace
  3807.   trace_pr_token;
  3808.   trace_pr_ln (' is a string global-variable');
  3809.   ecart@/
  3810. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  3811. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3812. check_for_already_seen_function (fn_loc);
  3813. fn_type[fn_loc] := str_global_var;@/
  3814. fn_info[fn_loc] := num_glb_strs;        {pointer into |global_strs|}
  3815. if (num_glb_strs = max_glob_strs) then
  3816.     overflow('number of string global-variables ',max_glob_strs);
  3817. incr(num_glb_strs);
  3818. @^gymnastics@>
  3819. That's it for processing \.{.bst} commands, except for finishing the
  3820. procedural gymnastics.  Note that this must topologically follow the
  3821. stuff for \.{.bib} reading, because that's done by the \.{.bst}'s
  3822. \.{read} command.
  3823. @<Procedures and functions for the reading and processing of input files@>=
  3824. @<Scan for and process a \.{.bst} command@>
  3825. @* Reading the database file(s).
  3826. This section reads the \.{.bib} file(s), each of which consists of a
  3827. sequence of entries (perhaps with a few \.{.bib} commands thrown in,
  3828. as explained later).  Each entry consists of an |at_sign|, an entry
  3829. type, and, between braces or parentheses and separated by |comma|s, a
  3830. database key and a list of fields.  Each field consists of a field
  3831. name, an |equals_sign|, and nonempty list of field tokens separated by
  3832. |concat_char|s.  Each field token is either a nonnegative number, a
  3833. macro name (like `jan'), or a brace-balanced string delimited by
  3834. either |double_quote|s or braces.  Finally, case differences are
  3835. ignored for all but delimited strings and database keys, and
  3836. |white_space| characters and ends-of-line may appear in all reasonable
  3837. places (i.e., anywhere except within entry types, database keys, field
  3838. names, and macro names); furthermore, comments may appear anywhere
  3839. between entries (or before the first or after the last) as long as
  3840. they contain no |at_sign|s.
  3841. These global variables are used while reading the \.{.bib} file(s).
  3842. The elements of |type_list|, which indicate an entry's type (book,
  3843. article, etc.), point either to a |hash_loc| or are one of two special
  3844. markers: |empty|, from which |hash_base = empty + 1| was defined,
  3845. means we haven't yet encountered the \.{.bib} entry corresponding to
  3846. this cite key; and |undefined| means we've encountered it but it had
  3847. an unknown entry type.  Thus the array |type_list| is of type
  3848. |hash_ptr2|, also defined earlier.  An element of the boolean array
  3849. |entry_exists| whose corresponding entry in |cite_list| gets
  3850. overwritten (which happens only when |all_entries| is |true|)
  3851. indicates whether we've encountered that entry of |cite_list| while
  3852. reading the \.{.bib} file(s); this information is unused for entries
  3853. that aren't (or more precisely, that have no chance of being)
  3854. overwritten.  When we're reading the database file, the array
  3855. |cite_info| contains auxiliary information for |cite_list|.  Later,
  3856. |cite_info| will become |sorted_cites|, and this dual role imposes the
  3857. (not-very-imposing) restriction |max_strings >= max_cites|.
  3858. @d undefined = hash_max + 1    {a special marker used for |type_list|}
  3859. @<Globals in the outer block@>=
  3860. @!bib_line_num : integer;    {line number of the \.{.bib} file}
  3861. @!entry_type_loc : hash_loc;    {the hash-table location of an entry type}
  3862. @!type_list : packed array[cite_number] of hash_ptr2;
  3863. @!type_exists : boolean;    {|true| if this entry type is \.{.bst}-defined}
  3864. @!entry_exists : packed array[cite_number] of boolean;
  3865. @!store_entry : boolean;    {|true| if we're to store info for this entry}
  3866. @!field_name_loc : hash_loc;    {the hash-table location of a field name}
  3867. @!field_val_loc : hash_loc;    {the hash-table location of a field value}
  3868. @!store_field : boolean;    {|true| if we're to store info for this field}
  3869. @!store_token : boolean;    {|true| if we're to store this macro token}
  3870. @!right_outer_delim : ASCII_code; {either a |right_brace| or a |right_paren|}
  3871. @!right_str_delim : ASCII_code; {either a |right_brace| or a |double_quote|}
  3872. @!at_bib_command : boolean;    {|true| for a command, false for an entry}
  3873. @!cur_macro_loc : hash_loc;    {|macro_loc| for a \.{string} being defined}
  3874. @!cite_info : packed array[cite_number] of str_number; {extra |cite_list| info}
  3875. @!cite_hash_found : boolean;    {set to a previous |hash_found| value}
  3876. @!preamble_ptr : bib_number;    {pointer into the |s_preamble| array}
  3877. @!num_preamble_strings : bib_number;    {counts the |s_preamble| strings}
  3878. This little procedure exists because it's used by at least two other
  3879. procedures and thus saves some space.
  3880. @<Procedures and functions for all file I/O, error messages, and such@>=
  3881. procedure bib_ln_num_print;
  3882. begin
  3883. print ('--line ',bib_line_num:0,' of file ');
  3884. print_bib_name;
  3885. When there's a serious error parsing a \.{.bib} file, we flush
  3886. everything up to the beginning of the next entry.
  3887. @d bib_err(#) == begin        {serious error during \.{.bib} parsing}
  3888.          print (#);
  3889.          bib_err_print;
  3890.          return;
  3891.          end
  3892. @<Procedures and functions for all file I/O, error messages, and such@>=
  3893. procedure bib_err_print;
  3894. begin
  3895. print ('-');
  3896. bib_ln_num_print;
  3897. print_bad_input_line;            {this call does the |mark_error|}
  3898. print_skipping_whatever_remains;
  3899. if (at_bib_command) then
  3900.     print_ln ('command')
  3901.   else
  3902.     print_ln ('entry');
  3903. When there's a harmless error parsing a \.{.bib} file, we just give a
  3904. warning message.  This is always called after other stuff has been
  3905. printed out.
  3906. @d bib_warn(#) == begin        {non-serious error during \.{.bst} parsing}
  3907.           print (#);
  3908.           bib_warn_print;
  3909.           end
  3910. @d bib_warn_newline(#) == begin        {same as above but with a newline}
  3911.               print_ln (#);
  3912.               bib_warn_print;
  3913.               end
  3914. @<Procedures and functions for all file I/O, error messages, and such@>=
  3915. procedure bib_warn_print;
  3916. begin
  3917. bib_ln_num_print;
  3918. mark_warning;
  3919. For all |num_bib_files| database files, we keep reading and processing
  3920. \.{.bib} entries until none left.
  3921. @<Read the \.{.bib} file(s)@>=
  3922. begin
  3923. @<Final initialization for \.{.bib} processing@>;
  3924. read_performed := true;
  3925. bib_ptr := 0;
  3926. while (bib_ptr < num_bib_files) do
  3927.     begin
  3928.     print ('Database file #',bib_ptr+1:0,': ');
  3929.     print_bib_name;@/
  3930.     bib_line_num := 0;        {initialize to get the first input line}
  3931.     buf_ptr2 := last;
  3932.     while (not eof(cur_bib_file)) do
  3933.     get_bib_command_or_entry_and_process;
  3934.     a_close (cur_bib_file);
  3935.     incr(bib_ptr);
  3936.     end;
  3937. reading_completed := true;
  3938.   trace
  3939.   trace_pr_ln ('Finished reading the database file(s)');
  3940.   ecart@/
  3941. @<Final initialization for processing the entries@>;
  3942. read_completed := true;
  3943. We need to initialize the |field_info| array, and also various things
  3944. associated with the |cite_list| array (but not |cite_list| itself).
  3945. @<Final initialization for \.{.bib} processing@>=
  3946. begin
  3947. @<Initialize the |field_info|@>;
  3948. @<Initialize things for the |cite_list|@>;
  3949. This module initializes all fields of all entries to |missing|, the
  3950. value to which all fields are initialized.
  3951. @<Initialize the |field_info|@>=
  3952. begin
  3953. check_field_overflow (num_fields*num_cites);
  3954. field_ptr := 0;
  3955. while (field_ptr < max_fields) do
  3956.     begin
  3957.     field_info[field_ptr] := missing;
  3958.     incr(field_ptr);
  3959.     end;
  3960. @^fetish@>
  3961. @:BibTeX capacity exceeded}{\quad total number of fields@>
  3962. Complain if somebody's got a field fetish.
  3963. @<Procedures and functions for all file I/O, error messages, and such@>=
  3964. procedure check_field_overflow (@!total_fields : integer);
  3965. begin
  3966. if (total_fields > max_fields) then
  3967.     begin
  3968.     print_ln (total_fields:0,' fields:');
  3969.     overflow('total number of fields ',max_fields);
  3970.     end;
  3971. We must initialize the |type_list| array so that we can detect
  3972. duplicate (or missing) entries for cite keys on |cite_list|.  Also,
  3973. when we're to include the entire database, we use the array
  3974. |entry_exists| to detect those missing entries whose |cite_list| info
  3975. will (or to be more precise, might) be overwritten; and we use the
  3976. array |cite_info| to save the part of |cite_list| that will (might) be
  3977. overwritten.  We also use |cite_info| for counting cross~references
  3978. when it's appropriate---when an entry isn't otherwise to be included
  3979. on |cite_list| (that is, the entry isn't \.{\\cite}d or
  3980. \.{\\nocite}d).  Such an entry is included on the final |cite_list| if
  3981. it's cross~referenced at least |min_crossrefs| times.
  3982. @<Initialize things for the |cite_list|@>=
  3983. begin
  3984. cite_ptr := 0;
  3985. while (cite_ptr < max_cites) do
  3986.     begin
  3987.     type_list[cite_ptr] := empty;@/
  3988.     cite_info[cite_ptr] := any_value;  {to appeas \PASCAL's boolean evaluation}
  3989.     incr(cite_ptr);
  3990.     end;
  3991. old_num_cites := num_cites;
  3992. if (all_entries) then
  3993.     begin
  3994.     cite_ptr := all_marker;
  3995.     while (cite_ptr < old_num_cites) do
  3996.     begin
  3997.     cite_info[cite_ptr] := cite_list[cite_ptr];
  3998.     entry_exists[cite_ptr] := false;
  3999.     incr(cite_ptr);
  4000.     end;
  4001.     cite_ptr := all_marker;    {we insert the ``other'' entries here}
  4002.     end
  4003.   else
  4004.     begin
  4005.     cite_ptr := num_cites;    {we insert the cross-referenced entries here}
  4006.     all_marker := any_value;    {to appease \PASCAL's boolean evaluation}
  4007.     end;
  4008. Before we actually start the code for reading a database file, we must
  4009. define this \.{.bib}-specific scanning function.  It skips over
  4010. |white_space| characters until hitting a nonwhite character or the end
  4011. of the file, respectively returning |true| or |false|.  It also
  4012. updates |bib_line_num|, the line counter.
  4013. @<Procedures and functions for input scanning@>=
  4014. function eat_bib_white_space : boolean;
  4015. label exit;
  4016. begin
  4017. while (not scan_white_space) do        {no characters left; read another line}
  4018.     begin
  4019.     if (not input_ln(cur_bib_file)) then    {end-of-file; return |false|}
  4020.     begin
  4021.     eat_bib_white_space := false;
  4022.     return;
  4023.     end;
  4024.     incr(bib_line_num);
  4025.     buf_ptr2 := 0;
  4026.     end;
  4027. eat_bib_white_space := true;
  4028. exit:
  4029. It's often illegal to end a \.{.bib} command in certain places, and
  4030. this is where we come to check.
  4031. @d eat_bib_white_and_eof_check ==
  4032.     begin
  4033.     if (not eat_bib_white_space) then
  4034.         begin
  4035.         eat_bib_print;
  4036.         return;
  4037.         end;
  4038. @<Procedures and functions for all file I/O, error messages, and such@>=
  4039. procedure eat_bib_print;
  4040. label exit;    {so the call to |bib_err| works}
  4041. begin
  4042. bib_err ('Illegal end of database file');
  4043. exit:
  4044. And here are a bunch of error-message macros, each called more than
  4045. once, that thus save space as implemented.  This one is for when one
  4046. of two possible characters is expected while scanning.
  4047. @d bib_one_of_two_expected_err(#) ==
  4048.     begin
  4049.     bib_one_of_two_print (#);
  4050.     return;
  4051. @<Procedures and functions for all file I/O, error messages, and such@>=
  4052. procedure bib_one_of_two_print (@!char1,@!char2:ASCII_code);
  4053. label exit;    {so the call to |bib_err| works}
  4054. begin
  4055. bib_err ('I was expecting a `',xchr[char1],''' or a `',xchr[char2],'''');
  4056. exit:
  4057. This one's for an expected |equals_sign|.
  4058. @d bib_equals_sign_expected_err ==
  4059.     begin
  4060.     bib_equals_sign_print;
  4061.     return;
  4062. @<Procedures and functions for all file I/O, error messages, and such@>=
  4063. procedure bib_equals_sign_print;
  4064. label exit;    {so the call to |bib_err| works}
  4065. begin
  4066. bib_err ('I was expecting an "',xchr[equals_sign],'"');
  4067. exit:
  4068. This complains about unbalanced braces.
  4069. @d bib_unbalanced_braces_err ==
  4070.     begin
  4071.     bib_unbalanced_braces_print;
  4072.     return;
  4073. @<Procedures and functions for all file I/O, error messages, and such@>=
  4074. procedure bib_unbalanced_braces_print;
  4075. label exit;    {so the call to |bib_err| works}
  4076. begin
  4077. bib_err ('Unbalanced braces');
  4078. exit:
  4079. And this one about an overly exuberant field.
  4080. @d bib_field_too_long_err ==
  4081.     begin
  4082.     bib_field_too_long_print;
  4083.     return;
  4084. @<Procedures and functions for all file I/O, error messages, and such@>=
  4085. procedure bib_field_too_long_print;
  4086. label exit;    {so the call to |bib_err| works}
  4087. begin
  4088. bib_err ('Your field is more than ',buf_size:0,' characters');
  4089. exit:
  4090. This one is just a warning, not an error.  It's for when something
  4091. isn't (or might not be) quite right with a macro name.
  4092. @d macro_name_warning(#) ==
  4093.     begin
  4094.     macro_warn_print;
  4095.     bib_warn_newline (#);
  4096. @<Procedures and functions for all file I/O, error messages, and such@>=
  4097. procedure macro_warn_print;
  4098. begin
  4099. print ('Warning--string name "');
  4100. print_token;
  4101. print ('" is ');
  4102. @:this can't happen}{\quad Identifier scanning error@>
  4103. This macro is used to scan all \.{.bib} identifiers.  The argument
  4104. tells what was happening at the time.  The associated procedure simply
  4105. prints an error message.
  4106. @d bib_identifier_scan_check(#) ==
  4107.     begin
  4108.     if ((scan_result = white_adjacent) or
  4109.                 (scan_result = specified_char_adjacent)) then
  4110.         do_nothing
  4111.     else
  4112.         begin
  4113.         bib_id_print;
  4114.         bib_err (#);
  4115.         end;
  4116. @<Procedures and functions for all file I/O, error messages, and such@>=
  4117. procedure bib_id_print;
  4118. begin
  4119. if (scan_result = id_null) then
  4120.     print ('You''re missing ')
  4121. else if (scan_result = other_char_adjacent) then
  4122.     print ('"',xchr[scan_char],'" immediately follows ')
  4123.     id_scanning_confusion;
  4124. This module either reads a database entry, whose three main components
  4125. are an entry type, a database key, and a list of fields, or it reads a
  4126. \.{.bib} command, whose structure is command dependent and explained
  4127. later.
  4128. @d cite_already_set = 22    {this gets around \PASCAL\ limitations}
  4129. @d first_time_entry = 26    {for checking for repeated database entries}
  4130. @<Scan for and process a \.{.bib} command or database entry@>=
  4131. procedure get_bib_command_or_entry_and_process;
  4132. label cite_already_set,@!first_time_entry,@!loop_exit,@!exit;
  4133. begin
  4134. at_bib_command := false;@/
  4135. @<Skip to the next database entry or \.{.bib} command@>;
  4136. @<Scan the entry type or scan and process the \.{.bib} command@>;
  4137. eat_bib_white_and_eof_check;
  4138. @<Scan the entry's database key@>;
  4139. eat_bib_white_and_eof_check;
  4140. @<Scan the entry's list of fields@>;
  4141. exit:
  4142. This module skips over everything until hitting an |at_sign| or the
  4143. end of the file.  It also updates |bib_line_num|, the line counter.
  4144. @<Skip to the next database entry or \.{.bib} command@>=
  4145. while (not scan1(at_sign)) do            {no |at_sign|; get next line}
  4146.     begin
  4147.     if (not input_ln(cur_bib_file)) then    {end-of-file}
  4148.     return;
  4149.     incr(bib_line_num);
  4150.     buf_ptr2 := 0;
  4151.     end
  4152. @:this can't happen}{\quad An at-sign disappeared@>
  4153. This module reads an |at_sign| and an entry type (like `book' or
  4154. `article') or a \.{.bib} command.  If it's an entry type, it must be
  4155. defined in the \.{.bst} file if this entry is to be included in the
  4156. reference list.
  4157. @<Scan the entry type or scan and process the \.{.bib} command@>=
  4158. begin
  4159. if (scan_char <> at_sign) then
  4160.     confusion ('An "',xchr[at_sign],'" disappeared');
  4161. incr(buf_ptr2);                    {skip over the |at_sign|}
  4162. eat_bib_white_and_eof_check;
  4163. scan_identifier (left_brace,left_paren,left_paren);
  4164. bib_identifier_scan_check ('an entry type');
  4165.   trace
  4166.   trace_pr_token;
  4167.   trace_pr_ln (' is an entry type or a database-file command');
  4168.   ecart@/
  4169. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  4170. command_num := ilk_info[
  4171.     str_lookup(buffer,buf_ptr1,token_len,bib_command_ilk,dont_insert)];
  4172. if (hash_found) then
  4173.     @<Process a \.{.bib} command@>
  4174.     begin                    {process an entry type}
  4175.     entry_type_loc := str_lookup(
  4176.             buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  4177.     if ((not hash_found) or (fn_type[entry_type_loc]<>wiz_defined)) then@/
  4178.     type_exists := false  {no such entry type defined in the \.{.bst} file}
  4179.       else
  4180.     type_exists := true;
  4181.     end;
  4182. @^database-file commands@>
  4183. @:this can't happen}{\quad Unknown database-file command@>
  4184. Here we determine which \.{.bib} command we're about to process, then
  4185. go to it.
  4186. @<Process a \.{.bib} command@>=
  4187. begin
  4188. at_bib_command := true;
  4189. case (command_num) of
  4190.     n_bib_comment : @<Process a \.{comment} command@>;
  4191.     n_bib_preamble : @<Process a \.{preamble} command@>;
  4192.     n_bib_string : @<Process a \.{string} command@>;
  4193.     othercases bib_cmd_confusion
  4194. endcases;
  4195. @:this can't happen}{\quad Unknown database-file command@>
  4196. Here's another bug.
  4197. @<Procedures and functions for all file I/O, error messages, and such@>=
  4198. procedure bib_cmd_confusion;
  4199. begin
  4200. confusion ('Unknown database-file command');
  4201. @:database-file commands}{\quad \.{comment}@>
  4202. The \.{comment} command is implemented for SCRIBE compatibility.  It's
  4203. not really needed because \BibTeX\ treats (flushes) everything not
  4204. within an entry as a comment anyway.
  4205. @<Process a \.{comment} command@>=
  4206. begin
  4207. return;            {flush comments}
  4208. @:database-file commands}{\quad \.{preamble}@>
  4209. The \.{preamble} command lets a user have \TeX\ stuff inserted (by the
  4210. standard styles, at least) directly into the \.{.bbl} file.  It is
  4211. intended primarily for allowing \TeX\ macro definitions used within
  4212. the bibliography entries (for better sorting, for example).  One
  4213. \.{preamble} command per \.{.bib} file should suffice.
  4214. A \.{preamble} command has either braces or parentheses as outer
  4215. delimiters.  Inside is the preamble string, which has the same syntax
  4216. as a field value: a nonempty list of field tokens separated by
  4217. |concat_char|s.  There are three types of field tokens---nonnegative
  4218. numbers, macro names, and delimited strings.
  4219. This module does all the scanning (that's not subcontracted), but the
  4220. \.{.bib}-specific scanning function
  4221. |scan_and_store_the_field_value_and_eat_white| actually stores the
  4222. value.
  4223. @<Process a \.{preamble} command@>=
  4224. begin
  4225. if (preamble_ptr = max_bib_files) then
  4226.     bib_err ('You''ve exceeded ',max_bib_files:0,' preamble commands');
  4227. eat_bib_white_and_eof_check;
  4228. if (scan_char = left_brace) then
  4229.     right_outer_delim := right_brace
  4230. else if (scan_char = left_paren) then
  4231.     right_outer_delim := right_paren
  4232.     bib_one_of_two_expected_err (left_brace,left_paren);
  4233. incr(buf_ptr2);                {skip over the left-delimiter}
  4234. eat_bib_white_and_eof_check;
  4235. store_field := true;
  4236. if (not scan_and_store_the_field_value_and_eat_white) then
  4237.     return;
  4238. if (scan_char <> right_outer_delim) then
  4239.     bib_err ('Missing "',xchr[right_outer_delim],'" in preamble command');
  4240. incr(buf_ptr2);                {skip over the |right_outer_delim|}
  4241. return;
  4242. @:database-file commands}{\quad \.{string}@>
  4243. The \.{string} command is implemented both for SCRIBE compatibility
  4244. and for allowing a user: to override a \.{.bst}-file \.{macro}
  4245. command, to define one that the \.{.bst} file doesn't, or to engage in
  4246. good, wholesome, typing laziness.
  4247. The \.{string} command does mostly the same thing as the
  4248. \.{.bst}-file's \.{macro} command (but the syntax is different and the
  4249. \.{string} command compresses |white_space|).  In fact, later in this
  4250. program, the term ``macro'' refers to either a \.{.bst} ``macro'' or a
  4251. \.{.bib} ``string'' (when it's clear from the context that it's not
  4252. a \.{WEB} macro).
  4253. A \.{string} command has either braces or parentheses as outer
  4254. delimiters.  Inside is the string's name (it must be a legal
  4255. identifier, and case differences are ignored---all upper-case letters
  4256. are converted to lower case), then an |equals_sign|, and the string's
  4257. definition, which has the same syntax as a field value: a nonempty
  4258. list of field tokens separated by |concat_char|s.  There are three
  4259. types of field tokens---nonnegative numbers, macro names, and
  4260. delimited strings.
  4261. @<Process a \.{string} command@>=
  4262. begin
  4263. eat_bib_white_and_eof_check;
  4264. @<Scan the string's name@>;
  4265. eat_bib_white_and_eof_check;
  4266. @<Scan the string's definition field@>;
  4267. return;
  4268. This module reads a left outer-delimiter and a string name.
  4269. @<Scan the string's name@>=
  4270. begin
  4271. if (scan_char = left_brace) then
  4272.     right_outer_delim := right_brace
  4273. else if (scan_char = left_paren) then
  4274.     right_outer_delim := right_paren
  4275.     bib_one_of_two_expected_err (left_brace,left_paren);
  4276. incr(buf_ptr2);                {skip over the left-delimiter}
  4277. eat_bib_white_and_eof_check;
  4278. scan_identifier (equals_sign,equals_sign,equals_sign);
  4279. bib_identifier_scan_check ('a string name');
  4280. @<Store the string's name@>;
  4281. @^commented-out code@>
  4282. This module marks this string as |macro_ilk|; the commented-out code
  4283. will give a warning message when overwriting a previously defined
  4284. macro.
  4285. @<Store the string's name@>=
  4286. begin
  4287.   trace
  4288.   trace_pr_token;
  4289.   trace_pr_ln (' is a database-defined macro');
  4290.   ecart@/
  4291. lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  4292. cur_macro_loc := str_lookup(buffer,buf_ptr1,token_len,macro_ilk,do_insert);
  4293. ilk_info[cur_macro_loc] := hash_text[cur_macro_loc]; {default in case of error}
  4294.   if (hash_found) then                {already seen macro}
  4295.       macro_name_warning ('having its definition overwritten');
  4296.   @}@/
  4297. This module skips over the |equals_sign|, reads and stores the list of
  4298. field tokens that defines this macro (compressing |white_space|), and
  4299. reads a |right_outer_delim|.
  4300. @<Scan the string's definition field@>=
  4301. begin
  4302. if (scan_char <> equals_sign) then
  4303.     bib_equals_sign_expected_err;
  4304. incr(buf_ptr2);                {skip over the |equals_sign|}
  4305. eat_bib_white_and_eof_check;
  4306. store_field := true;
  4307. if (not scan_and_store_the_field_value_and_eat_white) then
  4308.     return;
  4309. if (scan_char <> right_outer_delim) then
  4310.     bib_err ('Missing "',xchr[right_outer_delim],'" in string command');
  4311. incr(buf_ptr2);                {skip over the |right_outer_delim|}
  4312. @^kludge@>
  4313. The variables for the function
  4314. |scan_and_store_the_field_value_and_eat_white| must be global since
  4315. the functions it calls use them too.  The alias kludge helps make the
  4316. stack space not overflow on some machines.
  4317. @d field_vl_str == ex_buf    {aliases, used ``only'' for this function}
  4318. @d field_end == ex_buf_ptr    {the end marker for the field-value string}
  4319. @d field_start == ex_buf_xptr    {and the start marker}
  4320. @<Globals in the outer block@>=
  4321. @!bib_brace_level : integer;    {brace nesting depth (excluding |str_delim|s)}
  4322. @^gymnastics@>
  4323. Since the function |scan_and_store_the_field_value_and_eat_white|
  4324. calls several other yet-to-be-described functions (one directly and
  4325. two indirectly), we must perform some topological gymnastics.
  4326. @<Procedures and functions for input scanning@>=
  4327. @<The scanning function |compress_bib_white|@>@;
  4328. @<The scanning function |scan_balanced_braces|@>@;
  4329. @<The scanning function |scan_a_field_token_and_eat_white|@>
  4330. This function scans the list of field tokens that define the field
  4331. value string.  If |store_field| is |true| it accumulates (indirectly)
  4332. in |field_vl_str| the concatenation of all the field tokens,
  4333. compressing nonnull |white_space| to a single |space| and, if the
  4334. field value is for a field (rather than a string definition), removing
  4335. any leading or trailing |white_space|; when it's finished it puts the
  4336. string into the hash table.  It returns |false| if there was a serious
  4337. syntax error.
  4338. @<Procedures and functions for input scanning@>=
  4339. function scan_and_store_the_field_value_and_eat_white : boolean;
  4340. label exit;
  4341. begin
  4342. scan_and_store_the_field_value_and_eat_white := false;
  4343.                     {now it's easy to exit if necessary}
  4344. field_end := 0;
  4345. if (not scan_a_field_token_and_eat_white) then
  4346.     return;
  4347. while (scan_char = concat_char) do    {scan remaining field tokens}
  4348.     begin
  4349.     incr(buf_ptr2);            {skip over the |concat_char|}
  4350.     eat_bib_white_and_eof_check;
  4351.     if (not scan_a_field_token_and_eat_white) then
  4352.     return;
  4353.     end;
  4354. if (store_field) then
  4355.     @<Store the field value string@>;
  4356. scan_and_store_the_field_value_and_eat_white := true;
  4357. exit:
  4358. Each field token is either a nonnegative number, a macro name (like
  4359. `jan'), or a brace-balanced string delimited by either |double_quote|s
  4360. or braces.  Thus there are four possibilities for the first character
  4361. of the field token: If it's a |left_brace| or a |double_quote|, the
  4362. token (with balanced braces, up to the matching |right_str_delim|) is
  4363. a string; if it's |numeric|, the token is a number; if it's anything
  4364. else, the token is a macro name (and should thus have been defined by
  4365. either the \.{.bst}-file's \.{macro} command or the \.{.bib}-file's
  4366. \.{string} command).  This function returns |false| if there was a
  4367. serious syntax error.
  4368. @<The scanning function |scan_a_field_token_and_eat_white|@>=
  4369. function scan_a_field_token_and_eat_white : boolean;
  4370. label exit;
  4371. begin
  4372. scan_a_field_token_and_eat_white := false; {now it's easy to exit if necessary}
  4373. case (scan_char) of
  4374.     left_brace :
  4375.     begin
  4376.     right_str_delim := right_brace;
  4377.     if (not scan_balanced_braces) then
  4378.         return;
  4379.     end;
  4380.     double_quote :
  4381.     begin
  4382.     right_str_delim := double_quote;
  4383.     if (not scan_balanced_braces) then
  4384.         return;
  4385.     end;
  4386.     "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" :
  4387.     @<Scan a number@>;
  4388.     othercases
  4389.     @<Scan a macro name@>
  4390. endcases;
  4391. eat_bib_white_and_eof_check;
  4392. scan_a_field_token_and_eat_white := true;
  4393. exit:
  4394. Now we come to the stuff that actually accumulates the field value to
  4395. be stored.  This module copies a character into |field_vl_str| if it
  4396. will fit; since it's so low level, it's implemented as a macro.
  4397. @d copy_char(#) == begin
  4398.            if (field_end = buf_size) then
  4399.                bib_field_too_long_err
  4400.              else
  4401.                begin
  4402.                field_vl_str[field_end] := #;
  4403.                incr(field_end);
  4404.                end;
  4405.            end
  4406. The \.{.bib}-specific scanning function |compress_bib_white| skips
  4407. over |white_space| characters within a string until hitting a nonwhite
  4408. character; in fact, it does everything |eat_bib_white_space| does, but
  4409. it also adds a |space| to |field_vl_str|.  This function is never
  4410. called if there are no |white_space| characters (or ends-of-line) to
  4411. be scanned (though the associated macro might be).  The function
  4412. returns |false| if there is a serious syntax error.
  4413. @d check_for_and_compress_bib_white_space ==
  4414.     begin
  4415.     if ((lex_class[scan_char]=white_space) or (buf_ptr2=last)) then
  4416.         if (not compress_bib_white) then
  4417.         return;
  4418. @<The scanning function |compress_bib_white|@>=
  4419. function compress_bib_white : boolean;
  4420. label exit;
  4421. begin
  4422. compress_bib_white := false;        {now it's easy to exit if necessary}
  4423. copy_char (space);
  4424. while (not scan_white_space) do        {no characters left; read another line}
  4425.     begin
  4426.     if (not input_ln(cur_bib_file)) then    {end-of-file; complain}
  4427.     begin
  4428.     eat_bib_print;
  4429.     return;
  4430.     end;
  4431.     incr(bib_line_num);
  4432.     buf_ptr2 := 0;
  4433.     end;
  4434. compress_bib_white := true;
  4435. exit:
  4436. This \.{.bib}-specific function scans a string with balanced braces,
  4437. stopping just past the matching |right_str_delim|.  How much work it
  4438. does depends on whether |store_field = true|.  It returns |false| if
  4439. there was a serious syntax error.
  4440. @<The scanning function |scan_balanced_braces|@>=
  4441. function scan_balanced_braces : boolean;
  4442. label loop_exit,@!exit;
  4443. begin
  4444. scan_balanced_braces := false;        {now it's easy to exit if necessary}
  4445. incr(buf_ptr2);                {skip over the left-delimiter}
  4446. check_for_and_compress_bib_white_space;
  4447. if (field_end > 1) then
  4448.   if (field_vl_str[field_end-1] = space) then
  4449.     if (field_vl_str[field_end-2] = space) then    {remove wrongly added |space|}
  4450.     decr(field_end);
  4451. bib_brace_level := 0;        {and we're at a non|white_space| character}
  4452. if (store_field) then
  4453.     @<Do a full brace-balanced scan@>
  4454.   else
  4455.     @<Do a quick brace-balanced scan@>;
  4456. incr(buf_ptr2);                {skip over the |right_str_delim|}
  4457. scan_balanced_braces := true;
  4458. exit:
  4459. This module scans over a brace-balanced string without keeping track
  4460. of anything but the brace level.  It starts with |bib_brace_level = 0|
  4461. and at a non|white_space| character.
  4462. @<Do a quick brace-balanced scan@>=
  4463. begin
  4464. while (scan_char <> right_str_delim) do    {we're at |bib_brace_level = 0|}
  4465.     if (scan_char = left_brace) then
  4466.     begin
  4467.     incr(bib_brace_level);
  4468.     incr(buf_ptr2);            {skip over the |left_brace|}
  4469.     eat_bib_white_and_eof_check;
  4470.     while (bib_brace_level > 0) do
  4471.         @<Do a quick scan with |bib_brace_level > 0|@>;
  4472.     else if (scan_char = right_brace) then
  4473.     bib_unbalanced_braces_err
  4474.     else
  4475.     begin
  4476.     incr(buf_ptr2);            {skip over some other character}
  4477.     if (not scan3 (right_str_delim, left_brace, right_brace)) then
  4478.         eat_bib_white_and_eof_check;
  4479. This module does the same as above but, because |bib_brace_level > 0|, it
  4480. doesn't have to look for a |right_str_delim|.
  4481. @<Do a quick scan with |bib_brace_level > 0|@>=
  4482. begin    {top part of the |while| loop---we're always at a nonwhite character}
  4483. if (scan_char = right_brace) then
  4484.     begin
  4485.     decr(bib_brace_level);
  4486.     incr(buf_ptr2);            {skip over the |right_brace|}
  4487.     eat_bib_white_and_eof_check;
  4488.     end
  4489. else if (scan_char = left_brace) then
  4490.     begin
  4491.     incr(bib_brace_level);
  4492.     incr(buf_ptr2);            {skip over the |left_brace|}
  4493.     eat_bib_white_and_eof_check;
  4494.     end
  4495.     begin
  4496.     incr(buf_ptr2);            {skip over some other character}
  4497.     if (not scan2 (right_brace, left_brace)) then
  4498.     eat_bib_white_and_eof_check;
  4499.     end
  4500. This module scans over a brace-balanced string, compressing multiple
  4501. |white_space| characters into a single |space|.  It starts with
  4502. |bib_brace_level = 0| and starts at a non|white_space| character.
  4503. @<Do a full brace-balanced scan@>=
  4504. begin
  4505. while (scan_char <> right_str_delim) do
  4506.   case (scan_char) of
  4507.     left_brace :
  4508.     begin
  4509.     incr(bib_brace_level);
  4510.     copy_char (left_brace);@/
  4511.     incr(buf_ptr2);            {skip over the |left_brace|}
  4512.     check_for_and_compress_bib_white_space;@/
  4513.     @<Do a full scan with |bib_brace_level > 0|@>;
  4514.     end;
  4515.     right_brace :
  4516.     bib_unbalanced_braces_err;
  4517.     othercases
  4518.     begin
  4519.     copy_char (scan_char);
  4520.     incr(buf_ptr2);            {skip over some other character}
  4521.     check_for_and_compress_bib_white_space;
  4522.   endcases;
  4523. This module is similar to the last but starts with |bib_brace_level > 0|
  4524. (and, like the last, it starts at a non|white_space| character).
  4525. @<Do a full scan with |bib_brace_level > 0|@>=
  4526. begin
  4527.   case (scan_char) of
  4528.     right_brace :
  4529.     begin
  4530.     decr(bib_brace_level);
  4531.     copy_char (right_brace);@/
  4532.     incr(buf_ptr2);            {skip over the |right_brace|}
  4533.     check_for_and_compress_bib_white_space;
  4534.     if (bib_brace_level = 0) then
  4535.         goto loop_exit;
  4536.     end;
  4537.     left_brace :
  4538.     begin
  4539.     incr(bib_brace_level);
  4540.     copy_char (left_brace);@/
  4541.     incr(buf_ptr2);            {skip over the |left_brace|}
  4542.     check_for_and_compress_bib_white_space;
  4543.     end;
  4544.     othercases
  4545.     begin
  4546.     copy_char (scan_char);
  4547.     incr(buf_ptr2);            {skip over some other character}
  4548.     check_for_and_compress_bib_white_space;
  4549.   endcases;
  4550. loop_exit:
  4551. @:this can't happen}{\quad A digit disappeared@>
  4552. This module scans a nonnegative number and copies it to |field_vl_str|
  4553. if it's to store the field.
  4554. @<Scan a number@>=
  4555. begin
  4556. if (not scan_nonneg_integer) then
  4557.     confusion ('A digit disappeared');
  4558. if (store_field) then
  4559.     begin
  4560.     tmp_ptr := buf_ptr1;
  4561.     while (tmp_ptr < buf_ptr2) do
  4562.     begin
  4563.     copy_char (buffer[tmp_ptr]);
  4564.     incr(tmp_ptr);
  4565.     end;
  4566.     end;
  4567. This module scans a macro name and copies its string to |field_vl_str|
  4568. if it's to store the field, complaining if the macro is recursive or
  4569. undefined.
  4570. @<Scan a macro name@>=
  4571. begin
  4572. scan_identifier (comma,right_outer_delim,concat_char);
  4573. bib_identifier_scan_check ('a field part');
  4574. if (store_field) then
  4575.     begin
  4576.     lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  4577.     macro_name_loc := str_lookup(
  4578.             buffer,buf_ptr1,token_len,macro_ilk,dont_insert);
  4579.     store_token := true;
  4580.     if (at_bib_command) then
  4581.       if (command_num = n_bib_string) then
  4582.     if (macro_name_loc = cur_macro_loc) then
  4583.         begin
  4584.         store_token := false;
  4585.         macro_name_warning ('used in its own definition');
  4586.         end;
  4587.     if (not hash_found) then
  4588.     begin
  4589.     store_token := false;
  4590.     macro_name_warning ('undefined');
  4591.     end;
  4592.     if (store_token) then
  4593.     @<Copy the macro string to |field_vl_str|@>;
  4594.     end;
  4595. The macro definition may have |white_space| that needs compressing,
  4596. because it may have been defined in the \.{.bst} file.
  4597. @<Copy the macro string to |field_vl_str|@>=
  4598. begin
  4599. tmp_ptr := str_start[ilk_info[macro_name_loc]];
  4600. tmp_end_ptr := str_start[ilk_info[macro_name_loc]+1];
  4601. if (field_end = 0) then
  4602.   if ((lex_class[str_pool[tmp_ptr]] = white_space) and (tmp_ptr < tmp_end_ptr))
  4603.                                     then
  4604.     begin        {compress leading |white_space| of first nonnull token}
  4605.     copy_char (space);
  4606.     incr(tmp_ptr);
  4607.     while ((lex_class[str_pool[tmp_ptr]] = white_space) and
  4608.                         (tmp_ptr <  tmp_end_ptr)) do
  4609.     incr(tmp_ptr);
  4610.     end;        {the next remaining character is non|white_space|}
  4611. while (tmp_ptr < tmp_end_ptr) do
  4612.     begin
  4613.     if (lex_class[str_pool[tmp_ptr]] <> white_space) then
  4614.     copy_char (str_pool[tmp_ptr])
  4615.       else if (field_vl_str[field_end-1] <> space) then
  4616.     copy_char (space);
  4617.     incr(tmp_ptr);
  4618.     end;
  4619. @^ham and eggs@>
  4620. Now it's time to store the field value in the hash table, and store an
  4621. appropriate pointer to it (depending on whether it's for a database
  4622. entry or command).  But first, if necessary, we remove a trailing
  4623. |space| and a leading |space| if these exist.  (Hey, if we had some
  4624. ham we could make ham-and-eggs if we had some eggs.)
  4625. @<Store the field value string@>=
  4626. begin
  4627. if (not at_bib_command) then        {chop trailing |space| for a field}
  4628.   if (field_end > 0) then
  4629.     if (field_vl_str[field_end-1] = space) then
  4630.     decr(field_end);
  4631. if ((not at_bib_command) and (field_vl_str[0] = space) and (field_end > 0))
  4632.                 then    {chop leading |space| for a field}
  4633.     field_start := 1
  4634.   else
  4635.     field_start := 0;
  4636. field_val_loc := str_lookup(field_vl_str,field_start,field_end-field_start,
  4637.                             text_ilk,do_insert);
  4638. fn_type[field_val_loc] := str_literal;        {set the |fn_class|}
  4639.   trace
  4640.   trace_pr ('"');
  4641.   trace_pr_pool_str (hash_text[field_val_loc]);
  4642.   trace_pr_ln ('" is a field value');
  4643.   ecart@/
  4644. if (at_bib_command) then    {for a \.{preamble} or \.{string} command}
  4645.     @<Store the field value for a command@>
  4646.   else                            {for a database entry}
  4647.     @<Store the field value for a database entry@>;
  4648. @:this can't happen}{\quad Unknown database-file command@>
  4649. Here's where we store the goods when we're dealing with a command
  4650. rather than an entry.
  4651. @<Store the field value for a command@>=
  4652. begin
  4653. case (command_num) of
  4654.     n_bib_preamble :
  4655.     begin
  4656.     s_preamble[preamble_ptr] := hash_text[field_val_loc];
  4657.     incr(preamble_ptr);
  4658.     end;
  4659.     n_bib_string :
  4660.     ilk_info[cur_macro_loc] := hash_text[field_val_loc];
  4661.     othercases bib_cmd_confusion
  4662. endcases;
  4663. And here, an entry.
  4664. @<Store the field value for a database entry@>=
  4665. begin
  4666. field_ptr := entry_cite_ptr * num_fields + fn_info[field_name_loc];
  4667. if (field_info[field_ptr] <> missing) then
  4668.     begin
  4669.     print ('Warning--I''m ignoring ');
  4670.     print_pool_str (cite_list[entry_cite_ptr]);
  4671.     print ('''s extra "');
  4672.     print_pool_str (hash_text[field_name_loc]);
  4673.     bib_warn_newline ('" field');
  4674.     end
  4675.   else
  4676.     begin            {the field was empty, store its new value}
  4677.     field_info[field_ptr] := hash_text[field_val_loc];
  4678.     if ((fn_info[field_name_loc] = crossref_num) and (not all_entries)) then
  4679.     @<Add or update a cross reference on |cite_list| if necessary@>;
  4680.     end;
  4681. @^kludge@>
  4682. @:this can't happen}{\quad Cite hash error@>
  4683. If the cross-referenced entry isn't already on |cite_list| we add it
  4684. (at least temporarily); if it is already on |cite_list| we update the
  4685. cross-reference count, if necessary.  Note that |all_entries| is
  4686. |false| here.  The alias kludge helps make the stack space not
  4687. overflow on some machines.
  4688. @d extra_buf == out_buf        {an alias, used only in this module}
  4689. @<Add or update a cross reference on |cite_list| if necessary@>=
  4690. begin
  4691. tmp_ptr := field_start;
  4692. while (tmp_ptr < field_end) do
  4693.     begin
  4694.     extra_buf[tmp_ptr] := field_vl_str[tmp_ptr];
  4695.     incr(tmp_ptr);
  4696.     end;
  4697. lower_case (extra_buf, field_start, field_end-field_start);
  4698.                         {convert to `canonical' form}
  4699. lc_cite_loc := str_lookup(extra_buf,field_start,field_end-field_start,
  4700.                             lc_cite_ilk,do_insert);
  4701. if (hash_found) then
  4702.     begin
  4703.     cite_loc := ilk_info[lc_cite_loc];    {even if there's a case mismatch}
  4704.     if (ilk_info[cite_loc] >= old_num_cites) then  {a previous \.{crossref}}
  4705.     incr(cite_info[ilk_info[cite_loc]]);
  4706.     end
  4707.   else
  4708.     begin                    {it's a new \.{crossref}}
  4709.     cite_loc := str_lookup(field_vl_str,field_start,field_end-field_start,
  4710.                             cite_ilk,do_insert);
  4711.     if (hash_found) then
  4712.     hash_cite_confusion;
  4713.     add_database_cite (cite_ptr);        {this increments |cite_ptr|}
  4714.     cite_info[ilk_info[cite_loc]] := 1;    {the first cross-ref for this cite key}
  4715.     end;
  4716. This procedure adds (or restores) to |cite_list| a cite key; it is
  4717. called only when |all_entries| is |true| or when adding
  4718. cross~references, and it assumes that |cite_loc| and |lc_cite_loc| are
  4719. set.  It also increments its argument.
  4720. @<Procedures and functions for handling numbers, characters, and strings@>=
  4721. procedure add_database_cite (var new_cite : cite_number);
  4722. begin
  4723. check_cite_overflow (new_cite);            {make sure this cite will fit}
  4724. check_field_overflow (num_fields*new_cite);
  4725. cite_list[new_cite] := hash_text[cite_loc];
  4726. ilk_info[cite_loc] := new_cite;
  4727. ilk_info[lc_cite_loc] := cite_loc;
  4728. incr(new_cite);
  4729. And now, back to processing an entry (rather than a command).  This
  4730. module reads a left outer-delimiter and a database key.
  4731. @<Scan the entry's database key@>=
  4732. begin
  4733. if (scan_char = left_brace) then
  4734.     right_outer_delim := right_brace
  4735. else if (scan_char = left_paren) then
  4736.     right_outer_delim := right_paren
  4737.     bib_one_of_two_expected_err (left_brace,left_paren);
  4738. incr(buf_ptr2);                    {skip over the left-delimiter}
  4739. eat_bib_white_and_eof_check;
  4740. if (right_outer_delim = right_paren) then    {to allow it in a database key}
  4741.     begin
  4742.     if (scan1_white(comma)) then        {ok if database key ends line}
  4743.     do_nothing;
  4744.     end
  4745.   else
  4746.     if (scan2_white(comma,right_brace)) then {|right_brace=right_outer_delim|}
  4747.     do_nothing;
  4748. @<Check for a database key of interest@>;
  4749. @^kludge@>
  4750. The lower-case version of this database key must correspond to one in
  4751. |cite_list|, or else |all_entries| must be |true|, if this entry is to
  4752. be included in the reference list.  Accordingly, this module sets
  4753. |store_entry|, which determines whether the relevant information for
  4754. this entry is stored.  The alias kludge helps make the stack space not
  4755. overflow on some machines.
  4756. @d ex_buf3 == ex_buf        {an alias, used only in this module}
  4757. @<Check for a database key of interest@>=
  4758. begin
  4759.   trace
  4760.   trace_pr_token;
  4761.   trace_pr_ln (' is a database key');
  4762.   ecart@/
  4763. tmp_ptr := buf_ptr1;
  4764. while (tmp_ptr < buf_ptr2) do
  4765.     begin
  4766.     ex_buf3[tmp_ptr] := buffer[tmp_ptr];
  4767.     incr(tmp_ptr);
  4768.     end;
  4769. lower_case (ex_buf3, buf_ptr1, token_len);    {convert to `canonical' form}
  4770. if (all_entries) then
  4771.     lc_cite_loc := str_lookup(ex_buf3,buf_ptr1,token_len,lc_cite_ilk,do_insert)
  4772.   else
  4773.     lc_cite_loc := str_lookup(ex_buf3,buf_ptr1,token_len,lc_cite_ilk,
  4774.                                 dont_insert);
  4775. if (hash_found) then
  4776.     begin
  4777.     entry_cite_ptr := ilk_info[ilk_info[lc_cite_loc]];
  4778.     @<Check for a duplicate or \.{crossref}-matching database key@>;
  4779.     end;
  4780. store_entry := true;    {unless |(not hash_found) and (not all_entries)|}
  4781. if (all_entries) then
  4782.     @<Put this cite key in its place@>
  4783.   else if (not hash_found) then
  4784.     store_entry := false;    {no such cite key exists on |cite_list|}
  4785. if (store_entry) then
  4786.     @<Make sure this entry is ok before proceeding@>;
  4787. @:this can't happen}{\quad The cite list is messed up@>
  4788. It's illegal to have two (or more) entries with the same database key
  4789. (even if there are case differrences), and we skip the rest of the
  4790. entry for such a repeat occurrence.  Also, we make this entry's
  4791. database key the official |cite_list| key if it's on |cite_list| only
  4792. because of cross references.
  4793. @<Check for a duplicate or \.{crossref}-matching database key@>=
  4794. begin
  4795. if ((not all_entries) or (entry_cite_ptr < all_marker)
  4796.                 or (entry_cite_ptr >= old_num_cites)) then
  4797.     begin
  4798.     if (type_list[entry_cite_ptr] = empty) then
  4799.     begin
  4800.     @<Make sure this entry's database key is on |cite_list|@>;
  4801.     goto first_time_entry;
  4802.     end;
  4803.     end
  4804. else if (not entry_exists[entry_cite_ptr]) then
  4805.     begin
  4806.     @<Find the lower-case equivalent of the |cite_info| key@>;
  4807.     if (lc_xcite_loc = lc_cite_loc) then
  4808.     goto first_time_entry;
  4809.     end;@/
  4810.                 {oops---repeated entry---issue a reprimand}
  4811. if (type_list[entry_cite_ptr] = empty) then
  4812.     confusion ('The cite list is messed up');
  4813. bib_err ('Repeated entry');
  4814. first_time_entry:  {note that when we leave normally, |hash_found| is |true|}
  4815. An entry that's on |cite_list| only because of cross referencing must
  4816. have its database key (rather than one of the \.{crossref} keys) as
  4817. the official |cite_list| string.  Here's where we assure that.  The
  4818. variable |hash_found| is |true| upon entrance to and exit from this
  4819. module.
  4820. @<Make sure this entry's database key is on |cite_list|@>=
  4821. begin
  4822. if ((not all_entries) and (entry_cite_ptr >= old_num_cites)) then
  4823.     begin
  4824.     cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
  4825.     if (not hash_found) then
  4826.     begin            {it's not on |cite_list|---put it there}
  4827.     ilk_info[lc_cite_loc] := cite_loc;
  4828.     ilk_info[cite_loc] := entry_cite_ptr;
  4829.     cite_list[entry_cite_ptr] := hash_text[cite_loc];@/
  4830.     hash_found := true;        {restore this value for later use}
  4831.     end;
  4832.     end;
  4833. @^kludge@>
  4834. @:this can't happen}{\quad A cite key disappeared@>
  4835. This module, a simpler version of the
  4836. |find_cite_locs_for_this_cite_key| function, exists primarily to
  4837. compute |lc_xcite_loc|.  When this code is executed we have
  4838. |(all_entries) and (entry_cite_ptr >= all_marker) and (not
  4839. entry_exists[entry_cite_ptr])|.  The alias kludge helps make the stack
  4840. space not overflow on some machines.
  4841. @d ex_buf4 == ex_buf        {aliases, used only}
  4842. @d ex_buf4_ptr == ex_buf_ptr    {in this module}
  4843. @<Find the lower-case equivalent of the |cite_info| key@>=
  4844. begin
  4845. ex_buf4_ptr := 0;
  4846. tmp_ptr := str_start[cite_info[entry_cite_ptr]];
  4847. tmp_end_ptr := str_start[cite_info[entry_cite_ptr]+1];
  4848. while (tmp_ptr < tmp_end_ptr) do
  4849.     begin
  4850.     ex_buf4[ex_buf4_ptr] := str_pool[tmp_ptr];
  4851.     incr(ex_buf4_ptr);
  4852.     incr(tmp_ptr);
  4853.     end;
  4854. lower_case (ex_buf4, 0, length(cite_info[entry_cite_ptr]));
  4855.                         {convert to `canonical' form}
  4856. lc_xcite_loc := str_lookup(ex_buf4,0,length(cite_info[entry_cite_ptr]),
  4857.                         lc_cite_ilk,dont_insert);
  4858. if (not hash_found) then
  4859.     cite_key_disappeared_confusion;
  4860. @:this can't happen}{\quad A cite key disappeared@>
  4861. Here's another bug complaint.
  4862. @<Procedures and functions for all file I/O, error messages, and such@>=
  4863. procedure cite_key_disappeared_confusion;
  4864. begin
  4865. confusion ('A cite key disappeared');
  4866. @:this can't happen}{\quad Cite hash error@>
  4867. This module, which gets executed only when |all_entries| is |true|,
  4868. does one of three things, depending on whether or not, and where, the
  4869. cite key appears on |cite_list|: If it's on |cite_list| before
  4870. |all_marker|, there's nothing to be done; if it's after |all_marker|,
  4871. it must be reinserted (at the current place) and we must note that its
  4872. corresponding entry exists; and if it's not on |cite_list| at all, it
  4873. must be inserted for the first time.  The |goto| construct must stay
  4874. as is, partly because some \PASCAL\ compilers might complain if
  4875. ``|and|'' were to connect the two boolean expressions (since
  4876. |entry_cite_ptr| could be uninitialized when |hash_found| is |false|).
  4877. @<Put this cite key in its place@>=
  4878. begin
  4879. if (hash_found) then
  4880.     begin
  4881.     if (entry_cite_ptr < all_marker) then
  4882.     goto cite_already_set        {that is, do nothing}
  4883.       else
  4884.     begin
  4885.     entry_exists[entry_cite_ptr] := true;
  4886.     cite_loc := ilk_info[lc_cite_loc];
  4887.     end;
  4888.     end
  4889.   else
  4890.     begin                {this is a new key}
  4891.     cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
  4892.     if (hash_found) then
  4893.     hash_cite_confusion;
  4894.     end;@/
  4895. entry_cite_ptr := cite_ptr;
  4896. add_database_cite (cite_ptr);        {this increments |cite_ptr|}
  4897. cite_already_set:
  4898. @^case mismatch errors@>
  4899. @^commented-out code@>
  4900. We must give a warning if this entry~type doesn't exist.  Also, we
  4901. point the appropriate entry of |type_list| to the entry type just read
  4902. above.
  4903. For SCRIBE compatibility, the code to give a warning for a case
  4904. mismatch between a cite key and a database key has been commented out.
  4905. In fact, SCRIBE is the reason that it doesn't produce an error message
  4906. outright.  (Note: Case mismatches between two cite keys produce
  4907. full-blown errors.)
  4908. @<Make sure this entry is ok before proceeding@>=
  4909. begin
  4910.   dummy_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,dont_insert);
  4911.   if (not hash_found) then    {give a warning if there is a case difference}
  4912.     begin
  4913.     print ('Warning--case mismatch, database key "');
  4914.     print_token;
  4915.     print ('", cite key "');
  4916.     print_pool_str (cite_list[entry_cite_ptr]);
  4917.     bib_warn_newline ('"');
  4918.     end;
  4919.   @}@/
  4920. if (type_exists) then
  4921.     type_list[entry_cite_ptr] := entry_type_loc
  4922.   else
  4923.     begin
  4924.     type_list[entry_cite_ptr] := undefined;
  4925.     print ('Warning--entry type for "');
  4926.     print_token;
  4927.     bib_warn_newline ('" isn''t style-file defined');
  4928.     end;
  4929. This module reads a |comma| and a field as many times as it can, and
  4930. then reads a |right_outer_delim|, ending the current entry.
  4931. @<Scan the entry's list of fields@>=
  4932. begin
  4933. while (scan_char <> right_outer_delim) do
  4934.     begin
  4935.     if (scan_char <> comma) then
  4936.     bib_one_of_two_expected_err (comma,right_outer_delim);
  4937.     incr(buf_ptr2);            {skip over the |comma|}
  4938.     eat_bib_white_and_eof_check;
  4939.     if (scan_char = right_outer_delim) then
  4940.     goto loop_exit;
  4941.     @<Get the next field name@>;
  4942.     eat_bib_white_and_eof_check;
  4943.     if (not scan_and_store_the_field_value_and_eat_white) then
  4944.     return;
  4945.     end;
  4946. loop_exit:
  4947. incr(buf_ptr2);                {skip over the |right_outer_delim|}
  4948. This module reads a field name; its contents won't be stored unless it
  4949. was declared in the \.{.bst} file and |store_entry = true|.
  4950. @<Get the next field name@>=
  4951. begin
  4952. scan_identifier (equals_sign,equals_sign,equals_sign);
  4953. bib_identifier_scan_check ('a field name');
  4954.   trace
  4955.   trace_pr_token;
  4956.   trace_pr_ln (' is a field name');
  4957.   ecart@/
  4958. store_field := false;
  4959. if (store_entry) then
  4960.     begin
  4961.     lower_case (buffer, buf_ptr1, token_len);    {ignore case differences}
  4962.     field_name_loc := str_lookup(
  4963.             buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  4964.     if (hash_found) then
  4965.       if (fn_type[field_name_loc]=field) then@/
  4966.     store_field := true;  {field name was pre-defined or \.{.bst}-declared}
  4967.     end;
  4968. eat_bib_white_and_eof_check;
  4969. if (scan_char <> equals_sign) then
  4970.     bib_equals_sign_expected_err;
  4971. incr(buf_ptr2);            {skip over the |equals_sign|}
  4972. This gets things ready for further \.{.bst} processing.
  4973. @<Final initialization for processing the entries@>=
  4974. begin
  4975. num_cites := cite_ptr;    {to include database and \.{crossref} cite keys, too}
  4976. num_preamble_strings := preamble_ptr;    {number of \.{preamble} commands seen}
  4977. @<Add cross-reference information@>;
  4978. @<Subtract cross-reference information@>;
  4979. @<Remove missing entries or those cross referenced too few times@>;
  4980. @<Initialize the |int_entry_var|s@>;
  4981. @<Initialize the |str_entry_var|s@>;
  4982. @<Initialize the |sorted_cites|@>;
  4983. @^child entry@>
  4984. @^cross references@>
  4985. @^nested cross references@>
  4986. @^parent entry@>
  4987. Now we update any entry (here called a {\it child\/} entry) that
  4988. cross~referenced another (here called a {\it parent\/} entry); this
  4989. cross~referencing occurs when the child's \.{crossref} field (value)
  4990. consists of the parent's database key.  To do the update, we replace
  4991. the child's |missing| fields by the corresponding fields of the
  4992. parent.  Also, we make sure the \.{crossref} field contains the
  4993. case-correct version.  Finally, although it is technically illegal to
  4994. nest cross~references, and although we give a warning (a few modules
  4995. hence) when someone tries, we do what we can to accommodate the
  4996. attempt.
  4997. @<Add cross-reference information@>=
  4998. begin
  4999. cite_ptr := 0;
  5000. while (cite_ptr < num_cites) do
  5001.     begin
  5002.     field_ptr := cite_ptr * num_fields + crossref_num;
  5003.     if (field_info[field_ptr] <> missing) then
  5004.       if (find_cite_locs_for_this_cite_key (field_info[field_ptr])) then
  5005.     begin
  5006.     cite_loc := ilk_info[lc_cite_loc];
  5007.     field_info[field_ptr] := hash_text[cite_loc];
  5008.     cite_parent_ptr := ilk_info[cite_loc];
  5009.     field_ptr := cite_ptr * num_fields + num_pre_defined_fields;
  5010.     field_end_ptr := field_ptr - num_pre_defined_fields + num_fields;
  5011.     field_parent_ptr := cite_parent_ptr * num_fields
  5012.                         + num_pre_defined_fields;
  5013.     while (field_ptr < field_end_ptr) do
  5014.         begin
  5015.         if (field_info[field_ptr] = missing) then
  5016.         field_info[field_ptr] := field_info[field_parent_ptr];
  5017.         incr(field_ptr);
  5018.         incr(field_parent_ptr);
  5019.         end;
  5020.     end;
  5021.     incr(cite_ptr);
  5022.     end;
  5023. @^kludge@>
  5024. @^raisin@>
  5025. Occasionally we need to figure out the hash-table location of a given
  5026. cite-key string and its lower-case equivalent.  This function does
  5027. that.  To perform the task it needs to borrow a buffer, a need that
  5028. gives rise to the alias kludge---it helps make the stack space not
  5029. overflow on some machines (and while it's at it, it'll borrow a
  5030. pointer, too).  Finally, the function returns |true| if the cite key
  5031. exists on |cite_list|, and its sets |cite_hash_found| according to
  5032. whether or not it found the actual version (before |lower_case|ing) of
  5033. the cite key; however, its {\sl raison d'\^$\mkern-8mu$etre\/}
  5034. (literally, ``to eat a raisin'') is to compute |cite_loc| and
  5035. |lc_cite_loc|.
  5036. @d ex_buf5 == ex_buf        {aliases, used only}
  5037. @d ex_buf5_ptr == ex_buf_ptr    {in this module}
  5038. @<Procedures and functions for handling numbers, characters, and strings@>=
  5039. function find_cite_locs_for_this_cite_key (@!cite_str : str_number) : boolean;
  5040. begin
  5041. ex_buf5_ptr := 0;
  5042. tmp_ptr := str_start[cite_str];
  5043. tmp_end_ptr := str_start[cite_str+1];
  5044. while (tmp_ptr < tmp_end_ptr) do
  5045.     begin
  5046.     ex_buf5[ex_buf5_ptr] := str_pool[tmp_ptr];
  5047.     incr(ex_buf5_ptr);
  5048.     incr(tmp_ptr);
  5049.     end;
  5050. cite_loc := str_lookup(ex_buf5,0,length(cite_str),cite_ilk,dont_insert);
  5051. cite_hash_found := hash_found;
  5052. lower_case (ex_buf5, 0, length(cite_str));    {convert to `canonical' form}
  5053. lc_cite_loc := str_lookup(ex_buf5,0,length(cite_str),lc_cite_ilk,dont_insert);
  5054. if (hash_found) then
  5055.     find_cite_locs_for_this_cite_key := true
  5056.   else
  5057.     find_cite_locs_for_this_cite_key := false;
  5058. @:this can't happen}{\quad Cite hash error@>
  5059. Here we remove the \.{crossref} field value for each child whose
  5060. parent was cross~referenced too few times.  We also issue any
  5061. necessary warnings arising from a bad cross~reference.
  5062. @<Subtract cross-reference information@>=
  5063. begin
  5064. cite_ptr := 0;
  5065. while (cite_ptr < num_cites) do
  5066.     begin
  5067.     field_ptr := cite_ptr * num_fields + crossref_num;
  5068.     if (field_info[field_ptr] <> missing) then
  5069.       if (not find_cite_locs_for_this_cite_key (field_info[field_ptr])) then
  5070.     begin                {the parent is not on |cite_list|}
  5071.     if (cite_hash_found) then
  5072.         hash_cite_confusion;
  5073.     nonexistent_cross_reference_error;
  5074.     field_info[field_ptr] := missing;    {remove the \.{crossref} ptr}
  5075.       else
  5076.     begin                {the parent exists on |cite_list|}
  5077.     if (cite_loc <> ilk_info[lc_cite_loc]) then
  5078.         hash_cite_confusion;
  5079.     cite_parent_ptr := ilk_info[cite_loc];
  5080.     if (type_list[cite_parent_ptr] = empty) then
  5081.         begin
  5082.         nonexistent_cross_reference_error;@/
  5083.         field_info[field_ptr] := missing;    {remove the \.{crossref} ptr}
  5084.         end
  5085.       else
  5086.         begin            {the parent exists in the database too}
  5087.         field_parent_ptr := cite_parent_ptr * num_fields + crossref_num;
  5088.         if (field_info[field_parent_ptr] <> missing) then
  5089.         @<Complain about a nested cross reference@>;
  5090.         if ((not all_entries) and (cite_parent_ptr >= old_num_cites) and
  5091.             (cite_info[cite_parent_ptr] < min_crossrefs)) then@/
  5092.         field_info[field_ptr] := missing; {remove the \.{crossref} ptr}
  5093.         end;
  5094.     end;
  5095.     incr(cite_ptr);
  5096.     end;
  5097. This procedure exists to save space, since it's used twice---once for
  5098. each of the two succeeding modules.
  5099. @<Procedures and functions for all file I/O, error messages, and such@>=
  5100. procedure bad_cross_reference_print (@!s:str_number);
  5101. begin
  5102. print ('--entry "');
  5103. print_pool_str (cur_cite_str);
  5104. print_ln ('"');
  5105. print ('refers to entry "');
  5106. print_pool_str (s);
  5107. When an entry being cross referenced doesn't exist on |cite_list|, we
  5108. complain.
  5109. @<Procedures and functions for all file I/O, error messages, and such@>=
  5110. procedure nonexistent_cross_reference_error;
  5111. begin
  5112. print ('A bad cross reference-');
  5113. bad_cross_reference_print (field_info[field_ptr]);
  5114. print_ln ('", which doesn''t exist');
  5115. mark_error;
  5116. We also complain when an entry being cross referenced has a
  5117. non|missing| \.{crossref} field itself, but this one is just a
  5118. warning, not a full-blown error.
  5119. @<Complain about a nested cross reference@>=
  5120. begin
  5121. print ('Warning--you''ve nested cross references');
  5122. bad_cross_reference_print (cite_list[cite_parent_ptr]);
  5123. print_ln ('", which also refers to something');
  5124. mark_warning;
  5125. We remove (and give a warning for) each cite key on the original
  5126. |cite_list| without a corresponding database entry.  And we remove any
  5127. entry that was included on |cite_list| only because it was
  5128. cross~referenced, yet was cross~referenced fewer than |min_crossrefs|
  5129. times.  Throughout this module, |cite_ptr| points to the next cite key
  5130. to be checked and |cite_xptr| points to the next permanent spot on
  5131. |cite_list|.
  5132. @<Remove missing entries or those cross referenced too few times@>=
  5133. begin
  5134. cite_ptr := 0;
  5135. while (cite_ptr < num_cites) do
  5136.     begin
  5137.     if (type_list[cite_ptr] = empty) then
  5138.     print_missing_entry (cur_cite_str)
  5139.     else if ((all_entries) or (cite_ptr < old_num_cites) or
  5140.                 (cite_info[cite_ptr] >= min_crossrefs)) then
  5141.     begin
  5142.     if (cite_ptr > cite_xptr) then
  5143.         @<Slide this cite key down to its permanent spot@>;
  5144.     incr(cite_xptr);
  5145.     end;
  5146.     incr(cite_ptr);
  5147.     end;
  5148. num_cites := cite_xptr;
  5149. if (all_entries) then
  5150.     @<Complain about missing entries whose cite keys got overwritten@>;
  5151. When a cite key on the original |cite_list| (or added to |cite_list|
  5152. because of cross~referencing) didn't appear in the database, complain.
  5153. @<Procedures and functions for all file I/O, error messages, and such@>=
  5154. procedure print_missing_entry (@!s:str_number);
  5155. begin
  5156. print ('Warning--I didn''t find a database entry for "');
  5157. print_pool_str (s);
  5158. print_ln ('"');
  5159. mark_warning;
  5160. @:this can't happen}{\quad A cite key disappeared@>
  5161. @:this can't happen}{\quad Cite hash error@>
  5162. We have to move to its final resting place all the entry information
  5163. associated with the exact location in |cite_list| of this cite key.
  5164. @<Slide this cite key down to its permanent spot@>=
  5165. begin
  5166. cite_list[cite_xptr] := cite_list[cite_ptr];
  5167. type_list[cite_xptr] := type_list[cite_ptr];
  5168. if (not find_cite_locs_for_this_cite_key (cite_list[cite_ptr])) then
  5169.     cite_key_disappeared_confusion;
  5170. if ((not cite_hash_found) or (cite_loc <> ilk_info[lc_cite_loc])) then
  5171.     hash_cite_confusion;
  5172. ilk_info[cite_loc] := cite_xptr;@/
  5173. field_ptr := cite_xptr * num_fields;
  5174. field_end_ptr := field_ptr + num_fields;
  5175. tmp_ptr := cite_ptr * num_fields;
  5176. while (field_ptr < field_end_ptr) do
  5177.     begin
  5178.     field_info[field_ptr] := field_info[tmp_ptr];
  5179.     incr(field_ptr);
  5180.     incr(tmp_ptr);
  5181.     end;
  5182. We need this module only when we're including the whole database.
  5183. It's for missing entries whose cite key originally resided in
  5184. |cite_list| at a spot that another cite key (might have) claimed.
  5185. @<Complain about missing entries whose cite keys got overwritten@>=
  5186. begin
  5187. cite_ptr := all_marker;
  5188. while (cite_ptr < old_num_cites) do
  5189.     begin
  5190.     if (not entry_exists[cite_ptr]) then
  5191.     print_missing_entry (cite_info[cite_ptr]);
  5192.     incr(cite_ptr);
  5193.     end;
  5194. @:BibTeX capacity exceeded}{\quad total number of integer entry-variables@>
  5195. This module initializes all |int_entry_var|s of all entries to 0, the
  5196. value to which all integers are initialized.
  5197. @<Initialize the |int_entry_var|s@>=
  5198. begin
  5199. if (num_ent_ints*num_cites > max_ent_ints) then
  5200.     begin
  5201.     print (num_ent_ints*num_cites,': ');
  5202.     overflow('total number of integer entry-variables ',max_ent_ints);
  5203.     end;
  5204. int_ent_ptr := 0;
  5205. while (int_ent_ptr < num_ent_ints*num_cites) do
  5206.     begin
  5207.     entry_ints[int_ent_ptr] := 0;
  5208.     incr(int_ent_ptr);
  5209.     end;
  5210. @:BibTeX capacity exceeded}{\quad total number of string entry-variables@>
  5211. This module initializes all |str_entry_var|s of all entries to the
  5212. null string, the value to which all strings are initialized.
  5213. @<Initialize the |str_entry_var|s@>=
  5214. begin
  5215. if (num_ent_strs*num_cites > max_ent_strs) then
  5216.     begin
  5217.     print (num_ent_strs*num_cites,': ');
  5218.     overflow('total number of string entry-variables ',max_ent_strs);
  5219.     end;
  5220. str_ent_ptr := 0;
  5221. while (str_ent_ptr < num_ent_strs*num_cites) do
  5222.     begin
  5223.     entry_strs[str_ent_ptr][0] := end_of_string;
  5224.     incr(str_ent_ptr);
  5225.     end;
  5226. The array |sorted_cites| initially specifies that the entries are to
  5227. be processed in order of cite-key occurrence.  The \.{sort} command
  5228. may change this to whatever it likes (which, we hope, is whatever the
  5229. style-designer instructs it to like).  We make |sorted_cites| an alias
  5230. to save space; this works fine because we're done with |cite_info|.
  5231. @d sorted_cites == cite_info    {an alias used for the rest of the program}
  5232. @<Initialize the |sorted_cites|@>=
  5233. begin
  5234. cite_ptr := 0;
  5235. while (cite_ptr < num_cites) do
  5236.     begin
  5237.     sorted_cites[cite_ptr] := cite_ptr;
  5238.     incr(cite_ptr);
  5239.     end;
  5240. @* Executing the style file.
  5241. This part of the program produces the output by executing the
  5242. \.{.bst}-file commands \.{execute}, \.{iterate}, \.{reverse}, and
  5243. \.{sort}.  To do this it uses a stack (consisting of the two arrays
  5244. |lit_stack| and |lit_stk_type|) for storing literals, a buffer
  5245. |ex_buf| for manipulating strings, and an array |sorted_cites|
  5246. for holding pointers to the sorted cite keys (|sorted_cites| is an
  5247. alias of |cite_info|).
  5248. @<Globals in the outer block@>=
  5249. @!lit_stack : array[lit_stk_loc] of integer;    {the literal function stack}
  5250. @!lit_stk_type : array[lit_stk_loc] of stk_type; {their corresponding types}
  5251. @!lit_stk_ptr : lit_stk_loc;    {points just above the top of the stack}
  5252. @!cmd_str_ptr : str_number;    {stores value of |str_ptr| during execution}
  5253. @!ent_chr_ptr : 0..ent_str_size; {points at a |str_entry_var| character}
  5254. @!glob_chr_ptr : 0..glob_str_size; {points at a |str_global_var| character}
  5255. @!ex_buf : buf_type;        {a buffer for manipulating strings}
  5256. @!ex_buf_ptr : buf_pointer;    {general |ex_buf| location}
  5257. @!ex_buf_length : buf_pointer;    {the length of the current string in |ex_buf|}
  5258. @!out_buf : buf_type;        {the \.{.bbl} output buffer}
  5259. @!out_buf_ptr : buf_pointer;    {general |out_buf| location}
  5260. @!out_buf_length : buf_pointer;    {the length of the current string in |out_buf|}
  5261. @!mess_with_entries : boolean;    {|true| if functions can use entry info}
  5262. @!sort_cite_ptr : cite_number;    {a loop index for the sorted cite keys}
  5263. @!sort_key_num : str_ent_loc;    {index for the |str_entry_var| \.{sort.key\$}}
  5264. @!brace_level : integer;    {the brace nesting depth within a string}
  5265. Where |lit_stk_loc| is a stack location, and where |stk_type| gives
  5266. one of the three types of literals (an integer, a string, or a
  5267. function) or a special marker.  If a |lit_stk_type| element is a
  5268. |stk_int| then the corresponding |lit_stack| element is an integer; if
  5269. a |stk_str|, then a pointer to a |str_pool| string; and if a |stk_fn|,
  5270. then a pointer to the function's hash-table location.  However, if the
  5271. literal should have been a |stk_str| that was the value of a field
  5272. that happened to be |missing|, then the special value
  5273. |stk_field_missing| goes on the stack instead; its corresponding
  5274. |lit_stack| element is a pointer to the field-name's string.  Finally,
  5275. |stk_empty| is the type of a literal popped from an empty stack.
  5276. @d stk_int = 0        {an integer literal}
  5277. @d stk_str = 1        {a string literal}
  5278. @d stk_fn = 2        {a function literal}
  5279. @d stk_field_missing = 3 {a special marker: a field value was missing}
  5280. @d stk_empty = 4    {another: the stack was empty when this was popped}
  5281. @d last_lit_type = 4    {the same number as on the line above}
  5282. @<Types in the outer block@>=
  5283. @!lit_stk_loc = 0..lit_stk_size;    {the stack range}
  5284. @!stk_type = 0..last_lit_type;        {the literal types}
  5285. And the first output line requires this initialization.
  5286. @<Set initial values of key variables@>=
  5287. out_buf_length := 0;
  5288. When there's an error while executing \.{.bst} functions, what we do
  5289. depends on whether the function is messing with the entries.
  5290. Furthermore this error is serious enough to classify as an
  5291. |error_message| instead of a |warning_message|.  These messages (that
  5292. is, from |bst_ex_warn|) are meant both for the user and for the style
  5293. designer while debugging.
  5294. @d bst_ex_warn(#) == begin        {error while executing some function}
  5295.              print (#);
  5296.              bst_ex_warn_print;
  5297.              end
  5298. @<Procedures and functions for all file I/O, error messages, and such@>=
  5299. procedure bst_ex_warn_print;
  5300. begin
  5301. if (mess_with_entries) then
  5302.     begin
  5303.     print (' for entry ');
  5304.     print_pool_str (cur_cite_str);
  5305.     end;
  5306. print_newline;
  5307. print ('while executing-');
  5308. bst_ln_num_print;
  5309. mark_error;
  5310. When an error is so harmless, we print a |warning_message| instead of
  5311. an |error_message|.
  5312. @d bst_mild_ex_warn(#) == begin        {error while executing some function}
  5313.               print (#);
  5314.               bst_mild_ex_warn_print;
  5315.               end
  5316. @<Procedures and functions for all file I/O, error messages, and such@>=
  5317. procedure bst_mild_ex_warn_print;
  5318. begin
  5319. if (mess_with_entries) then
  5320.     begin
  5321.     print (' for entry ');
  5322.     print_pool_str (cur_cite_str);
  5323.     end;
  5324. print_newline;
  5325. bst_warn ('while executing');            {This does the |mark_warning|}
  5326. It's illegal to mess with the entry information at certain times;
  5327. here's a complaint for these times.
  5328. @<Procedures and functions for all file I/O, error messages, and such@>=
  5329. procedure bst_cant_mess_with_entries_print;
  5330. begin
  5331. bst_ex_warn ('You can''t mess with entries here');
  5332. This module executes a single specified function once.  It can't do
  5333. anything with the entries.
  5334. @<Perform an \.{execute} command@>=
  5335. begin
  5336. init_command_execution;
  5337. mess_with_entries := false;
  5338. execute_fn (fn_loc);
  5339. check_command_execution;
  5340. This module iterates a single specified function for all entries
  5341. specified by |cite_list|.
  5342. @<Perform an \.{iterate} command@>=
  5343. begin
  5344. init_command_execution;
  5345. mess_with_entries := true;
  5346. sort_cite_ptr := 0;
  5347. while (sort_cite_ptr < num_cites) do
  5348.     begin
  5349.     cite_ptr := sorted_cites[sort_cite_ptr];
  5350.       trace
  5351.       trace_pr_pool_str (hash_text[fn_loc]);
  5352.       trace_pr (' to be iterated on ');
  5353.       trace_pr_pool_str (cur_cite_str);
  5354.       trace_pr_newline;
  5355.       ecart@/
  5356.     execute_fn (fn_loc);
  5357.     check_command_execution;
  5358.     incr(sort_cite_ptr);
  5359.     end;
  5360. This module iterates a single specified function for all entries
  5361. specified by |cite_list|, but does it in reverse order.
  5362. @<Perform a \.{reverse} command@>=
  5363. begin
  5364. init_command_execution;
  5365. mess_with_entries := true;
  5366. if (num_cites > 0) then
  5367.     begin
  5368.     sort_cite_ptr := num_cites;
  5369.     repeat
  5370.     decr(sort_cite_ptr);
  5371.     cite_ptr := sorted_cites[sort_cite_ptr];
  5372.       trace
  5373.       trace_pr_pool_str (hash_text[fn_loc]);
  5374.       trace_pr (' to be iterated in reverse on ');
  5375.       trace_pr_pool_str (cur_cite_str);
  5376.       trace_pr_newline;
  5377.       ecart@/
  5378.     execute_fn (fn_loc);
  5379.     check_command_execution;
  5380.       until (sort_cite_ptr = 0);
  5381.     end;
  5382. This module sorts the entries based on \.{sort.key\$}; it is a stable
  5383. sort.
  5384. @<Perform a \.{sort} command@>=
  5385. begin
  5386.   trace
  5387.   trace_pr_ln ('Sorting the entries');
  5388.   ecart@/
  5389. if (num_cites > 1) then
  5390.     quick_sort (0, num_cites-1);
  5391.   trace
  5392.   trace_pr_ln ('Done sorting');
  5393.   ecart@/
  5394. These next two procedures (actually, one procedures and one function,
  5395. but who's counting) are subroutines for |quick_sort|, which follows.
  5396. The |swap| procedure exchanges the two elements its arguments point
  5397. @<Procedures and functions for handling numbers, characters, and strings@>=
  5398. procedure swap (@!swap1,@!swap2 : cite_number);
  5399. var innocent_bystander : cite_number;    {the temporary element in an exchange}
  5400. begin
  5401. innocent_bystander := sorted_cites[swap2];
  5402. sorted_cites[swap2] := sorted_cites[swap1];
  5403. sorted_cites[swap1] := innocent_bystander;
  5404. @:this can't happen}{\quad Duplicate sort key@>
  5405. The function |less_than| compares the two \.{sort.key\$}s indirectly
  5406. pointed to by its arguments and returns |true| if the first argument's
  5407. \.{sort.key\$} is lexicographically less than the second's (that is,
  5408. alphabetically earlier).  In case of ties the function compares the
  5409. indices |arg1| and |arg2|, which are assumed to be different, and
  5410. returns |true| if the first is smaller.  This function uses
  5411. |ASCII_code|s to compare, so it might give ``interesting'' results
  5412. when handling nonletters.
  5413. @d compare_return(#) == begin        {the compare is finished}
  5414.             less_than := #;
  5415.             return;
  5416.             end
  5417. @<Procedures and functions for handling numbers, characters, and strings@>=
  5418. function less_than (@!arg1,@!arg2 : cite_number) : boolean;
  5419. label exit;
  5420. var char_ptr : 0..ent_str_size;        {character index into compared strings}
  5421.     @!ptr1,@!ptr2 : str_ent_loc;    {the two \.{sort.key\$} pointers}
  5422.     @!char1,@!char2 : ASCII_code;    {the two characters being compared}
  5423. begin
  5424. ptr1 := arg1*num_ent_strs + sort_key_num;
  5425. ptr2 := arg2*num_ent_strs + sort_key_num;
  5426. char_ptr := 0;
  5427.     begin
  5428.     char1 := entry_strs[ptr1][char_ptr];
  5429.     char2 := entry_strs[ptr2][char_ptr];
  5430.     if (char1 = end_of_string) then
  5431.     if (char2 = end_of_string) then
  5432.         if (arg1 < arg2) then
  5433.         compare_return (true)
  5434.         else if (arg1 > arg2) then
  5435.         compare_return (false)
  5436.         else                {|arg1 = arg2|}
  5437.         confusion ('Duplicate sort key')
  5438.     else                    {|char2 <> end_of_string|}
  5439.         compare_return (true)
  5440.     else                    {|char1 <> end_of_string|}
  5441.     if (char2 = end_of_string) then
  5442.         compare_return (false)
  5443.     else if (char1 < char2) then
  5444.     compare_return (true)
  5445.     else if (char1 > char2) then
  5446.     compare_return (false);
  5447.     incr(char_ptr);
  5448.     end;
  5449. exit:
  5450. The recursive procedure |quick_sort| sorts the entries indirectly
  5451. pointed to by the |sorted_cites| elements between |left_end| and
  5452. |right_end|, inclusive, based on the value of the |str_entry_var|
  5453. \.{sort.key\$}.  It's a fairly standard quicksort (for example, see
  5454. Algorithm 5.2.2Q in {\sl The Art of Computer Programming}), but uses
  5455. the median-of-three method to choose the partition element just in
  5456. case the entries are already sorted (or nearly sorted---humans and
  5457. ASCII might have different ideas on lexicographic ordering); it is a
  5458. stable sort.  This code generally prefers clarity to assembler-type
  5459. execution-time efficiency since |cite_list|s will rarely be huge.
  5460. The value |short_list|, which must be at least |2*end_offset + 2| for
  5461. this code to work, tells us the list-length at which the list is small
  5462. enough to warrant switching over to straight insertion sort from the
  5463. recursive quicksort.  The values here come from modest empirical tests
  5464. aimed at minimizing, for large |cite_list|s (five hundred or so), the
  5465. number of comparisons (between keys) plus the number of calls to
  5466. |quick_sort|.  The value |end_offset| must be positive; this helps
  5467. avoid $n^2$ behavior observed when the list starts out nearly, but not
  5468. completely, sorted (and fairly frequently large |cite_list|s come from
  5469. entire databases, which fairly frequently are nearly sorted).
  5470. @d short_list = 10    {use straight insertion sort at or below this length}
  5471. @d end_offset = 4    {the index end-offsets for choosing a median-of-three}
  5472. @<Check the ``constant'' values for consistency@>=
  5473. if (short_list < 2*end_offset + 2) then    bad:=100*bad+22;
  5474. Here's the actual procedure.
  5475. @d next_insert = 24    {now insert the next element}
  5476. @<Procedures and functions for handling numbers, characters, and strings@>=
  5477. procedure quick_sort (@!left_end,@!right_end : cite_number);
  5478. label next_insert;
  5479. var left,@!right : cite_number;        {two general |sorted_cites| pointers}
  5480.     @!insert_ptr : cite_number;        {the to-be-(straight)-inserted element}
  5481.     @!middle : cite_number;    {the |(left_end+right_end) div 2| element}
  5482.     @!partition : cite_number;        {the median-of-three partition element}
  5483. begin
  5484.   trace
  5485.   trace_pr_ln ('Sorting ',left_end:0,' through ',right_end:0);
  5486.   ecart@/
  5487. if (right_end - left_end < short_list) then
  5488.     @<Do a straight insertion sort@>
  5489.   else
  5490.     begin
  5491.     @<Draw out the median-of-three partition element@>;
  5492.     @<Do the partitioning and the recursive calls@>;
  5493.     end;
  5494. This code sorts the entries between |left_end| and |right_end| when
  5495. the difference is less than |short_list|.  Each iteration of the outer
  5496. loop inserts the element indicated by |insert_ptr| into its proper
  5497. place among the (sorted) elements from |left_end| through
  5498. |insert_ptr-1|.
  5499. @<Do a straight insertion sort@>=
  5500. begin
  5501. for insert_ptr := left_end+1 to right_end do
  5502.     begin
  5503.     for right := insert_ptr downto left_end+1 do
  5504.     begin
  5505.     if (less_than (sorted_cites[right-1], sorted_cites[right])) then
  5506.         goto next_insert;
  5507.     swap (right-1, right);
  5508.     end;
  5509. next_insert:
  5510.     end;
  5511. Now we find the median of the three \.{sort.key\$}s to which the three
  5512. elements |sorted_cites[left_end+end_offset]|,
  5513. |sorted_cites[right_end]-end_offset|, and
  5514. |sorted_cites[(left_end+right_end) div 2]| point (a nonzero
  5515. |end_offset| avoids using as the leftmost of the three elements the
  5516. one that was swapped there when the old partition element was swapped
  5517. into its final spot; this turns out to avoid $n^2$ behavior when the
  5518. list is nearly sorted to start with).  This code determines which of
  5519. the six possible permutations we're dealing with and moves the median
  5520. element to |left_end|.  The comments next to the |swap| actions give
  5521. the known orderings of the corresponding elements of |sorted_cites|
  5522. before the action.
  5523. @<Draw out the median-of-three partition element@>=
  5524. begin
  5525. left := left_end + end_offset;
  5526. middle := (left_end+right_end) div 2;
  5527. right := right_end - end_offset;
  5528. if (less_than (sorted_cites[left], sorted_cites[middle])) then
  5529.   if (less_than (sorted_cites[middle], sorted_cites[right])) then
  5530.                     {|left < middle < right|}
  5531.     swap(left_end,middle)
  5532.     else if (less_than (sorted_cites[left], sorted_cites[right])) then
  5533.                     {|left < right < middle|}
  5534.     swap(left_end,right)
  5535.       else                {|right < left < middle|}
  5536.     swap(left_end,left)
  5537.   else if (less_than (sorted_cites[right], sorted_cites[middle])) then
  5538.                     {|right < middle < left|}
  5539.     swap(left_end,middle)
  5540.     else if (less_than (sorted_cites[right], sorted_cites[left])) then
  5541.                     {|middle < right < left|}
  5542.     swap(left_end,right)
  5543.       else                {|middle < left < right|}
  5544.     swap(left_end,left);
  5545. This module uses the median-of-three computed above to partition the
  5546. elements into those less than and those greater than the median.
  5547. Equal \.{sort.key\$}s are sorted by order of occurrence (in
  5548. |cite_list|).
  5549. @<Do the partitioning and the recursive calls@>=
  5550. begin
  5551. partition := sorted_cites[left_end];
  5552. left := left_end + 1;
  5553. right := right_end;
  5554. repeat
  5555.     while (less_than (sorted_cites[left], partition)) do
  5556.     incr(left);
  5557.     while (less_than (partition, sorted_cites[right])) do
  5558.     decr(right);
  5559.         {now |sorted_cites[right] < partition < sorted_cites[left]|}
  5560.     if (left < right) then
  5561.     begin
  5562.     swap (left,right);
  5563.     incr(left);
  5564.     decr(right);
  5565.     end;
  5566. until (left = right+1);    {pointers have crossed}
  5567. swap (left_end,right);{restoring the partition element to its |right|ful place}
  5568. quick_sort (left_end,right-1);
  5569. quick_sort (left,right_end);
  5570. @:BibTeX capacity exceeded}{\quad literal-stack size@>
  5571. @:this can't happen}{\quad Unknown literal type@>
  5572. Ok, that's it for sorting; now we'll play with the literal stack.
  5573. This procedure pushes a literal onto the stack, checking for stack
  5574. overflow.
  5575. @<Procedures and functions for style-file function execution@>=
  5576. procedure push_lit_stk (@!push_lt:integer; @!push_type:stk_type);
  5577.   trace
  5578.   var dum_ptr : lit_stk_loc;    {used just as an index variable}
  5579.   ecart@/
  5580. begin
  5581. lit_stack[lit_stk_ptr] := push_lt;
  5582. lit_stk_type[lit_stk_ptr] := push_type;
  5583.   trace
  5584.   for dum_ptr := 0 to lit_stk_ptr do
  5585.     trace_pr ('  ');
  5586.   trace_pr ('Pushing ');
  5587.   case (lit_stk_type[lit_stk_ptr]) of
  5588.     stk_int : trace_pr_ln (lit_stack[lit_stk_ptr]:0);
  5589.     stk_str : begin
  5590.           trace_pr ('"');
  5591.           trace_pr_pool_str (lit_stack[lit_stk_ptr]);
  5592.           trace_pr_ln ('"');
  5593.           end;
  5594.     stk_fn : begin
  5595.          trace_pr ('`');
  5596.          trace_pr_pool_str (hash_text[lit_stack[lit_stk_ptr]]);
  5597.          trace_pr_ln ('''');
  5598.          end;
  5599.     stk_field_missing : begin
  5600.             trace_pr ('missing field `');
  5601.             trace_pr_pool_str (lit_stack[lit_stk_ptr]);
  5602.             trace_pr_ln ('''');
  5603.             end;
  5604.     stk_empty : trace_pr_ln ('a bad literal--popped from an empty stack');
  5605.     othercases unknwn_literal_confusion
  5606.   endcases;
  5607.   ecart@/
  5608. if (lit_stk_ptr = lit_stk_size) then
  5609.     overflow('literal-stack size ',lit_stk_size);
  5610. incr(lit_stk_ptr);
  5611. @^push the literal stack@>
  5612. This macro pushes the last thing, necessarily a string, that was
  5613. popped.  And this module, along with others that push the literal
  5614. stack without explicitly calling |push_lit_stack|, have an index entry
  5615. under ``push the literal stack''; these implicit pushes collectively
  5616. speed up the program by about ten percent.
  5617. @d repush_string == begin
  5618.             if (lit_stack[lit_stk_ptr] >= cmd_str_ptr) then
  5619.             unflush_string;
  5620.             incr(lit_stk_ptr);
  5621.             end
  5622. @:this can't happen}{\quad Nontop top of string stack@>
  5623. This procedure pops the stack, checking for, and trying to recover
  5624. from, stack underflow.  (Actually, this procedure is really a
  5625. function, since it returns the two values through its |var|
  5626. parameters.)  Also, if the literal being popped is a |stk_str| that's
  5627. been created during the execution of the current \.{.bst} command, pop
  5628. it from |str_pool| as well (it will be the string corresponding to
  5629. |str_ptr-1|).  Note that when this happens, the string is no longer
  5630. `officially' available so that it must be used before anything else is
  5631. added to |str_pool|.
  5632. @<Procedures and functions for style-file function execution@>=
  5633. procedure pop_lit_stk (var pop_lit:integer; var pop_type:stk_type);
  5634. begin
  5635. if (lit_stk_ptr = 0) then
  5636.     begin
  5637.     bst_ex_warn ('You can''t pop an empty literal stack');@/
  5638.     pop_type := stk_empty;    {this is an error recovery attempt}
  5639.     end
  5640.   else
  5641.     begin
  5642.     decr(lit_stk_ptr);
  5643.     pop_lit := lit_stack[lit_stk_ptr];
  5644.     pop_type := lit_stk_type[lit_stk_ptr];
  5645.     if (pop_type = stk_str) then
  5646.       if (pop_lit >= cmd_str_ptr) then
  5647.     begin
  5648.     if (pop_lit <> str_ptr-1) then
  5649.         confusion ('Nontop top of string stack');
  5650.     flush_string;
  5651.     end;
  5652.     end;
  5653. @:this can't happen}{\quad Illegal literal type@>
  5654. @:this can't happen}{\quad Unknown literal type@>
  5655. More bug complaints, this time about bad literals.
  5656. @<Procedures and functions for all file I/O, error messages, and such@>=
  5657. procedure illegl_literal_confusion;
  5658. begin
  5659. confusion ('Illegal literal type');
  5660. procedure unknwn_literal_confusion;
  5661. begin
  5662. confusion ('Unknown literal type');
  5663. @:this can't happen}{\quad Illegal literal type@>
  5664. @:this can't happen}{\quad Unknown literal type@>
  5665. Occasionally we'll want to know what's on the literal stack.  Here we
  5666. print out a stack literal, giving its type.  This procedure should
  5667. never be called after popping an empty stack.
  5668. @<Procedures and functions for all file I/O, error messages, and such@>=
  5669. procedure print_stk_lit (@!stk_lt:integer; @!stk_tp:stk_type);
  5670. begin
  5671. case (stk_tp) of
  5672.     stk_int : print (stk_lt:0,' is an integer literal');
  5673.     stk_str : begin
  5674.           print ('"');
  5675.           print_pool_str (stk_lt);
  5676.           print ('" is a string literal');
  5677.           end;
  5678.     stk_fn : begin
  5679.          print ('`');
  5680.          print_pool_str (hash_text[stk_lt]);
  5681.          print (''' is a function literal');
  5682.          end;
  5683.     stk_field_missing : begin
  5684.             print ('`');
  5685.             print_pool_str (stk_lt);
  5686.             print (''' is a missing field');
  5687.             end;
  5688.     stk_empty : illegl_literal_confusion;
  5689.     othercases unknwn_literal_confusion
  5690. endcases;
  5691. @:this can't happen}{\quad Illegal literal type@>
  5692. @:this can't happen}{\quad Unknown literal type@>
  5693. This procedure appropriately chastises the style designer; however, if
  5694. the wrong literal came from popping an empty stack, the procedure
  5695. |pop_lit_stack| will have already done the chastising (because this
  5696. procedure is called only after popping the stack) so there's no need
  5697. for more.
  5698. @<Procedures and functions for style-file function execution@>=
  5699. procedure print_wrong_stk_lit (@!stk_lt:integer; @!stk_tp1,@!stk_tp2:stk_type);
  5700. begin
  5701. if (stk_tp1 <> stk_empty) then
  5702.     begin
  5703.     print_stk_lit (stk_lt, stk_tp1);
  5704.     case (stk_tp2) of
  5705.     stk_int : print (', not an integer,');
  5706.     stk_str : print (', not a string,');
  5707.     stk_fn : print (', not a function,');
  5708.     stk_field_missing,
  5709.     stk_empty : illegl_literal_confusion;
  5710.     othercases unknwn_literal_confusion
  5711.     endcases;
  5712.     bst_ex_warn_print;
  5713.     end;
  5714. @:this can't happen}{\quad Illegal literal type@>
  5715. @:this can't happen}{\quad Unknown literal type@>
  5716. This is similar to |print_stk_lit|, but here we don't give the
  5717. literal's type, and here we end with a new line.  This procedure
  5718. should never be called after popping an empty stack.
  5719. @<Procedures and functions for all file I/O, error messages, and such@>=
  5720. procedure print_lit (@!stk_lt:integer; @!stk_tp:stk_type);
  5721. begin
  5722. case (stk_tp) of
  5723.     stk_int : print_ln (stk_lt:0);
  5724.     stk_str : begin
  5725.           print_pool_str (stk_lt);
  5726.           print_newline;
  5727.           end;
  5728.     stk_fn : begin
  5729.          print_pool_str (hash_text[stk_lt]);
  5730.          print_newline;
  5731.          end;
  5732.     stk_field_missing : begin
  5733.             print_pool_str (stk_lt);
  5734.             print_newline;
  5735.             end;
  5736.     stk_empty : illegl_literal_confusion;
  5737.     othercases unknwn_literal_confusion
  5738. endcases;
  5739. This procedure pops and prints the top of the stack; when the stack is
  5740. empty the procedure |pop_lit_stk| complains.
  5741. @<Procedures and functions for style-file function execution@>=
  5742. procedure pop_top_and_print;
  5743. var stk_lt : integer;
  5744.   @!stk_tp : stk_type;
  5745. begin
  5746. pop_lit_stk (stk_lt,stk_tp);
  5747. if (stk_tp = stk_empty) then
  5748.     print_ln ('Empty literal')
  5749.   else
  5750.     print_lit (stk_lt,stk_tp);
  5751. This procedure pops and prints the whole stack.
  5752. @<Procedures and functions for style-file function execution@>=
  5753. procedure pop_whole_stack;
  5754. begin
  5755. while (lit_stk_ptr > 0) do
  5756.     pop_top_and_print;
  5757. At the beginning of a \.{.bst}-command execution we make the stack
  5758. empty and record how much of |str_pool| has been used.
  5759. @<Procedures and functions for style-file function execution@>=
  5760. procedure init_command_execution;
  5761. begin
  5762. lit_stk_ptr := 0;    {make the stack empty}
  5763. cmd_str_ptr := str_ptr;    {we'll check this when we finish command execution}
  5764. @:this can't happen}{\quad Nonempty empty string stack@>
  5765. At the end of a \.{.bst} command-execution we check that the stack and
  5766. |str_pool| are still in good shape.
  5767. @<Procedures and functions for style-file function execution@>=
  5768. procedure check_command_execution;
  5769. begin
  5770. if (lit_stk_ptr<>0) then
  5771.     begin
  5772.     print_ln ('ptr=',lit_stk_ptr:0,', stack=');
  5773.     pop_whole_stack;
  5774.     bst_ex_warn ('---the literal stack isn''t empty');
  5775.     end;
  5776. if (cmd_str_ptr<>str_ptr) then
  5777.     begin
  5778.       trace
  5779.       print_ln ('Pointer is ',str_ptr:0,' but should be ',cmd_str_ptr:0);
  5780.       ecart@/
  5781.     confusion ('Nonempty empty string stack');
  5782.     end;
  5783. This procedure adds to |str_pool| the string from |ex_buf[0]| through
  5784. |ex_buf[ex_buf_length-1]| if it will fit.  It assumes the global
  5785. variable |ex_buf_length| gives the length of the current string in
  5786. |ex_buf|.  It then pushes this string onto the literal stack.
  5787. @<Procedures and functions for style-file function execution@>=
  5788. procedure add_pool_buf_and_push;
  5789. begin
  5790. str_room (ex_buf_length);        {make sure this string will fit}
  5791. ex_buf_ptr := 0;
  5792. while (ex_buf_ptr < ex_buf_length) do
  5793.     begin
  5794.     append_char (ex_buf[ex_buf_ptr]);
  5795.     incr(ex_buf_ptr);
  5796.     end;
  5797. push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  5798. @:BibTeX capacity exceeded}{\quad buffer size@>
  5799. These macros append a character to |ex_buf|.  Which is called depends
  5800. on whether the character is known to fit.
  5801. @d append_ex_buf_char(#) == begin
  5802.                 ex_buf[ex_buf_ptr] := #;
  5803.                 incr(ex_buf_ptr);
  5804.                 end
  5805. @d append_ex_buf_char_and_check(#) ==
  5806.                 begin
  5807.                 if (ex_buf_ptr = buf_size) then
  5808.                 buffer_overflow;
  5809.                 append_ex_buf_char(#);
  5810.                 end
  5811. @:BibTeX capacity exceeded}{\quad buffer size@>
  5812. This procedure adds to the execution buffer the given string in
  5813. |str_pool| if it will fit.  It assumes the global variable
  5814. |ex_buf_length| gives the length of the current string in |ex_buf|,
  5815. and thus also gives the location of the next character.
  5816. @<Procedures and functions for style-file function execution@>=
  5817. procedure add_buf_pool (@!p_str : str_number);
  5818. begin
  5819. p_ptr1 := str_start[p_str];
  5820. p_ptr2 := str_start[p_str+1];
  5821. if (ex_buf_length+(p_ptr2-p_ptr1) > buf_size) then
  5822.     buffer_overflow;
  5823. ex_buf_ptr := ex_buf_length;
  5824. while (p_ptr1 < p_ptr2) do
  5825.     begin            {copy characters into the buffer}
  5826.     append_ex_buf_char (str_pool[p_ptr1]);
  5827.     incr(p_ptr1);
  5828.     end;
  5829. ex_buf_length := ex_buf_ptr;
  5830. This procedure actually writes onto the \.{.bbl}~file a line of output
  5831. (the characters from |out_buf[0]| to |out_buf[out_buf_length-1]|,
  5832. after removing trailing |white_space| characters).  It also updates
  5833. |bbl_line_num|, the line counter.  It writes a blank line if and only
  5834. if |out_buf| is empty.  The program uses this procedure in such a way
  5835. that |out_buf| will be nonempty if there have been characters put in
  5836. it since the most recent \.{newline\$}.
  5837. @<Procedures and functions for all file I/O, error messages, and such@>=
  5838. procedure output_bbl_line;
  5839. label loop_exit,@!exit;
  5840. begin
  5841. if (out_buf_length <> 0) then        {the buffer's not empty}
  5842.     begin
  5843.     while (out_buf_length > 0) do    {remove trailing |white_space|}
  5844.       if (lex_class[out_buf[out_buf_length-1]] = white_space) then
  5845.     decr(out_buf_length)
  5846.        else
  5847.     goto loop_exit;
  5848. loop_exit:
  5849.     if (out_buf_length = 0) then    {ignore a line of just |white_space|}
  5850.     return;
  5851.     out_buf_ptr := 0;
  5852.     while (out_buf_ptr < out_buf_length) do
  5853.     begin
  5854.     write (bbl_file, xchr[out_buf[out_buf_ptr]]);
  5855.     incr(out_buf_ptr);
  5856.     end;
  5857.     end;
  5858. write_ln (bbl_file);
  5859. incr(bbl_line_num);    {update line number}
  5860. out_buf_length := 0;    {make the next line empty}
  5861. exit:
  5862. @:BibTeX capacity exceeded}{\quad output buffer size@>
  5863. This procedure adds to the output buffer the given string in
  5864. |str_pool|.  It assumes the global variable |out_buf_length| gives the
  5865. length of the current string in |out_buf|, and thus also gives the
  5866. location for the next character.  If there are enough characters
  5867. present in the output buffer, it writes one or more lines out to the
  5868. \.{.bbl} file.  It may break a line at any |white_space| character it
  5869. likes, but if it does, it will add two |space|s to the next output
  5870. line.
  5871. @<Procedures and functions for style-file function execution@>=
  5872. procedure add_out_pool (@!p_str : str_number);
  5873. var break_ptr : buf_pointer;    {the first character following the line break}
  5874. @!end_ptr : buf_pointer;    {temporary end-of-buffer pointer}
  5875. begin
  5876. p_ptr1 := str_start[p_str];
  5877. p_ptr2 := str_start[p_str+1];
  5878. if (out_buf_length+(p_ptr2-p_ptr1) > buf_size) then
  5879.     overflow('output buffer size ',buf_size);
  5880. out_buf_ptr := out_buf_length;
  5881. while (p_ptr1 < p_ptr2) do
  5882.     begin            {copy characters into the buffer}
  5883.     out_buf[out_buf_ptr] := str_pool[p_ptr1];
  5884.     incr(p_ptr1);
  5885.     incr(out_buf_ptr);
  5886.     end;
  5887. out_buf_length := out_buf_ptr;
  5888. while (out_buf_length > max_print_line) do
  5889.     @<Break that line@>;
  5890. Here we break the line by looking for a |white_space| character,
  5891. backwards from |out_buf[max_print_line]| until
  5892. |out_buf[min_print_line]|; we break at the |white_space| and indent
  5893. the next line two |space|s.  The next module handles things when
  5894. there's no |white_space| character to break at.
  5895. @<Break that line@>=
  5896. begin
  5897. end_ptr := out_buf_length;
  5898. out_buf_ptr := max_print_line;
  5899. while ((lex_class[out_buf[out_buf_ptr]] <> white_space) and
  5900.                     (out_buf_ptr >= min_print_line)) do
  5901.     decr(out_buf_ptr);
  5902. if (out_buf_ptr = min_print_line-1) then    {no |white_space| character}
  5903.     @<Break that unbreakable line@>
  5904.     begin                    {hit a |white_space| character}
  5905.     out_buf_length := out_buf_ptr;
  5906.     break_ptr := out_buf_length + 1;
  5907.     output_bbl_line;            {output what we can}
  5908.     out_buf[0] := space;
  5909.     out_buf[1] := space;        {start the next line with two |space|s}
  5910.     out_buf_ptr := 2;
  5911.     tmp_ptr := break_ptr;
  5912.     while (tmp_ptr < end_ptr) do    {and slide the rest down}
  5913.     begin
  5914.     out_buf[out_buf_ptr] := out_buf[tmp_ptr];
  5915.     incr(out_buf_ptr);
  5916.     incr(tmp_ptr);
  5917.     end;
  5918.     out_buf_length := end_ptr - break_ptr + 2;
  5919.     end;
  5920. If there's no |white_space| character to break the line at, we break
  5921. it at |out_buf[max_print_line-1]|, append a |comment| character, and
  5922. don't indent the next line.
  5923. @<Break that unbreakable line@>=
  5924. begin
  5925. out_buf[end_ptr] := out_buf[max_print_line-1];    {save this character}
  5926. out_buf[max_print_line-1] := comment;        {so \TeX\ does the thing right}
  5927. out_buf_length := max_print_line;
  5928. break_ptr := out_buf_length - 1;    {the `|-1|' allows for the restoration}
  5929. output_bbl_line;                {output what we can,}
  5930. out_buf[max_print_line-1] := out_buf[end_ptr];    {restore this character}
  5931. out_buf_ptr := 0;
  5932. tmp_ptr := break_ptr;
  5933. while (tmp_ptr < end_ptr) do            {and slide the rest down}
  5934.     begin
  5935.     out_buf[out_buf_ptr] := out_buf[tmp_ptr];
  5936.     incr(out_buf_ptr);
  5937.     incr(tmp_ptr);
  5938.     end;
  5939. out_buf_length := end_ptr - break_ptr;
  5940. @^Tuesdays@>
  5941. @^windows@>
  5942. @:this can't happen}{\quad Unknown function class@>
  5943. This procedure executes a single specified function; it is the single
  5944. execution-primitive that does everything (except windows, and it takes
  5945. Tuesdays off).
  5946. @<|execute_fn| itself@>=
  5947. procedure execute_fn (@!ex_fn_loc : hash_loc);
  5948. @<Declarations for executing |built_in| functions@>
  5949. @!wiz_ptr : wiz_fn_loc;        {general |wiz_functions| location}
  5950. begin
  5951.   trace
  5952.   trace_pr ('execute_fn `');
  5953.   trace_pr_pool_str (hash_text[ex_fn_loc]);
  5954.   trace_pr_ln ('''');
  5955.   ecart@/
  5956. case (fn_type[ex_fn_loc]) of
  5957.     built_in : @<Execute a |built_in| function@>;
  5958.     wiz_defined : @<Execute a |wiz_defined| function@>;
  5959.     int_literal : push_lit_stk (fn_info[ex_fn_loc], stk_int);
  5960.     str_literal : push_lit_stk (hash_text[ex_fn_loc], stk_str);
  5961.     field : @<Execute a field@>;
  5962.     int_entry_var : @<Execute an |int_entry_var|@>;
  5963.     str_entry_var : @<Execute a |str_entry_var|@>;
  5964.     int_global_var : push_lit_stk (fn_info[ex_fn_loc], stk_int);
  5965.     str_global_var : @<Execute a |str_global_var|@>;
  5966.     othercases unknwn_function_class_confusion
  5967. endcases;
  5968. To execute a |wiz_defined| function, we just execute all those
  5969. functions in its definition, except that the special marker
  5970. |quote_next_fn| means we push the next function onto the stack.
  5971. @<Execute a |wiz_defined| function@>=
  5972. begin
  5973. wiz_ptr := fn_info[ex_fn_loc];
  5974. while (wiz_functions[wiz_ptr] <> end_of_def) do
  5975.     begin
  5976.     if (wiz_functions[wiz_ptr] <> quote_next_fn) then
  5977.     execute_fn (wiz_functions[wiz_ptr])
  5978.       else
  5979.     begin
  5980.     incr(wiz_ptr);
  5981.     push_lit_stk (wiz_functions[wiz_ptr], stk_fn);
  5982.     end;
  5983.     incr(wiz_ptr);
  5984.     end;
  5985. This module pushes the string given by the field onto the literal
  5986. stack unless it's |missing|, in which case it pushes a special value
  5987. onto the stack.
  5988. @<Execute a field@>=
  5989. begin
  5990. if (not mess_with_entries) then
  5991.     bst_cant_mess_with_entries_print
  5992.   else
  5993.     begin
  5994.     field_ptr := cite_ptr*num_fields + fn_info[ex_fn_loc];
  5995.     if (field_info[field_ptr] = missing) then
  5996.     push_lit_stk (hash_text[ex_fn_loc], stk_field_missing)
  5997.       else
  5998.     push_lit_stk (field_info[field_ptr], stk_str);
  5999.     end
  6000. This module pushes the integer given by an |int_entry_var| onto the
  6001. literal stack.
  6002. @<Execute an |int_entry_var|@>=
  6003. begin
  6004. if (not mess_with_entries) then
  6005.     bst_cant_mess_with_entries_print
  6006.   else
  6007.     push_lit_stk (entry_ints[cite_ptr*num_ent_ints+fn_info[ex_fn_loc]],
  6008.                                 stk_int);
  6009. This module adds the string given by a |str_entry_var| to |str_pool|
  6010. via the execution buffer and pushes it onto the literal stack.
  6011. @<Execute a |str_entry_var|@>=
  6012. begin
  6013. if (not mess_with_entries) then
  6014.     bst_cant_mess_with_entries_print
  6015.   else
  6016.     begin
  6017.     str_ent_ptr := cite_ptr*num_ent_strs + fn_info[ex_fn_loc];@/
  6018.     ex_buf_ptr := 0;            {also serves as |ent_chr_ptr|}
  6019.     while (entry_strs[str_ent_ptr][ex_buf_ptr] <> end_of_string) do
  6020.                     {copy characters into the buffer}
  6021.     append_ex_buf_char (entry_strs[str_ent_ptr][ex_buf_ptr]);
  6022.     ex_buf_length := ex_buf_ptr;
  6023.     add_pool_buf_and_push;        {push this string onto the stack}
  6024.     end;
  6025. This module pushes the string given by a |str_global_var| onto the
  6026. literal stack, but it copies the string to |str_pool| (character by
  6027. character) only if it has to---it {\it doesn't\/} have to if the
  6028. string is static (that is, if the string isn't at the top, temporary
  6029. part of the string pool).
  6030. @<Execute a |str_global_var|@>=
  6031. begin
  6032. str_glb_ptr := fn_info[ex_fn_loc];
  6033. if (glb_str_ptr[str_glb_ptr] > 0) then    {we're dealing with a static string}
  6034.     push_lit_stk (glb_str_ptr[str_glb_ptr],stk_str)
  6035.   else
  6036.     begin
  6037.     str_room(glb_str_end[str_glb_ptr]);
  6038.     glob_chr_ptr := 0;
  6039.     while (glob_chr_ptr < glb_str_end[str_glb_ptr]) do    {copy the string}
  6040.     begin
  6041.     append_char (global_strs[str_glb_ptr][glob_chr_ptr]);
  6042.     incr(glob_chr_ptr);
  6043.     end;
  6044.     push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  6045.     end;
  6046. @* The built-in functions.
  6047. @^add a built-in function@>
  6048. @^biblical procreation@>
  6049. @^grade inflation@>
  6050. This section gives the all the code for all the built-in functions
  6051. (including pre-defined |field|s, |str_entry_var|s, and
  6052. |int_global_var|s, which technically aren't classified as |built_in|).
  6053. To modify or add one, we needn't go anywhere else (with one exception:
  6054. The constant |max_pop|, which gives the maximum number of literals
  6055. that any of these functions pops off the stack, is defined earlier
  6056. because it's needed earlier; thus, if we need to update it, which will
  6057. happen if some new |built_in| functions uses more than |max_pop|
  6058. literals from the stack, we'll have to go outside this section).
  6059. Adding a |built_in| function entails modifying (at least four of) the
  6060. five modules marked by ``add a built-in function'' in the index, in
  6061. addition to adding the code to execute the function.
  6062. These variables all begin with |b_| and specify the hash-table
  6063. locations of the |built_in| functions, except that |b_default| is
  6064. pseudo-|built_in|---either it will point to the no-op \.{skip\$} or to
  6065. the \.{.bst}-defined function \.{default.type}; it's used when an
  6066. entry has a type that's not defined in the \.{.bst} file.
  6067. @<Globals in the outer block@>=
  6068. @!b_equals : hash_loc;        {\.{=}}
  6069. @!b_greater_than : hash_loc;    {\.{>}}
  6070. @!b_less_than : hash_loc;    {\.{<}}
  6071. @!b_plus : hash_loc;        {\.{+} (this may be changed to an |a_minus|)}
  6072. @!b_minus : hash_loc;        {\.{-}}
  6073. @!b_concatenate : hash_loc;    {\.{*}}
  6074. @!b_gets : hash_loc;        {\.{:=} (formerly, |b_gat|)}
  6075. @!b_add_period : hash_loc;    {\.{add.period\$}}
  6076. @!b_call_type : hash_loc;    {\.{call.type\$}}
  6077. @!b_change_case : hash_loc;    {\.{change.case\$}}
  6078. @!b_chr_to_int : hash_loc;    {\.{chr.to.int\$}}
  6079. @!b_cite : hash_loc;        {\.{cite\$}}
  6080. @!b_duplicate : hash_loc;    {\.{duplicate\$}}
  6081. @!b_empty : hash_loc;        {\.{empty\$}}
  6082. @!b_format_name : hash_loc;    {\.{format.name\$}}
  6083. @!b_if : hash_loc;        {\.{if\$}}
  6084. @!b_int_to_chr : hash_loc;    {\.{int.to.chr\$}}
  6085. @!b_int_to_str : hash_loc;    {\.{int.to.str\$}}
  6086. @!b_missing : hash_loc;        {\.{missing\$}}
  6087. @!b_newline : hash_loc;        {\.{newline\$}}
  6088. @!b_num_names : hash_loc;    {\.{num.names\$}}
  6089. @!b_pop : hash_loc;        {\.{pop\$}}
  6090. @!b_preamble : hash_loc;    {\.{preamble\$}}
  6091. @!b_purify : hash_loc;        {\.{purify\$}}
  6092. @!b_quote : hash_loc;        {\.{quote\$}}
  6093. @!b_skip : hash_loc;        {\.{skip\$}}
  6094. @!b_stack : hash_loc;        {\.{stack\$}}
  6095. @!b_substring : hash_loc;    {\.{substring\$}}
  6096. @!b_swap : hash_loc;        {\.{swap\$}}
  6097. @!b_text_length : hash_loc;    {\.{text.length\$}}
  6098. @!b_text_prefix : hash_loc;    {\.{text.prefix\$}}
  6099. @!b_top_stack : hash_loc;    {\.{top\$}}
  6100. @!b_type : hash_loc;        {\.{type\$}}
  6101. @!b_warning : hash_loc;        {\.{warning\$}}
  6102. @!b_while : hash_loc;        {\.{while\$}}
  6103. @!b_width : hash_loc;        {\.{width\$}}
  6104. @!b_write : hash_loc;        {\.{write\$}}
  6105. @!b_default : hash_loc;        {either \.{skip\$} or \.{default.type}}
  6106.   stat
  6107.   @!blt_in_loc : array[blt_in_range] of hash_loc; {for execution counts}
  6108.   @!execution_count : array[blt_in_range] of integer; {the same}
  6109.   @!total_ex_count : integer;        {the sum of all |execution_count|s}
  6110.   @!blt_in_ptr : blt_in_range;        {a pointer into |blt_in_loc|}
  6111.   tats@/
  6112. Where |blt_in_range| gives the legal |built_in| function numbers.
  6113. @<Types in the outer block@>=
  6114. @!blt_in_range = 0..num_blt_in_fns;
  6115. @^add a built-in function@>
  6116. These constants all begin with |n_| and are used for the |case|
  6117. statement that determines which |built_in| function to execute.
  6118. @d n_equals = 0        {\.{=}}
  6119. @d n_greater_than = 1    {\.{>}}
  6120. @d n_less_than = 2    {\.{<}}
  6121. @d n_plus = 3        {\.{+}}
  6122. @d n_minus = 4        {\.{-}}
  6123. @d n_concatenate = 5    {\.{*}}
  6124. @d n_gets = 6        {\.{:=}}
  6125. @d n_add_period = 7    {\.{add.period\$}}
  6126. @d n_call_type = 8    {\.{call.type\$}}
  6127. @d n_change_case = 9    {\.{change.case\$}}
  6128. @d n_chr_to_int = 10    {\.{chr.to.int\$}}
  6129. @d n_cite = 11        {\.{cite\$} (this may start a riot)}
  6130. @d n_duplicate = 12    {\.{duplicate\$}}
  6131. @d n_empty = 13        {\.{empty\$}}
  6132. @d n_format_name = 14    {\.{format.name\$}}
  6133. @d n_if = 15        {\.{if\$}}
  6134. @d n_int_to_chr = 16    {\.{int.to.chr\$}}
  6135. @d n_int_to_str = 17    {\.{int.to.str\$}}
  6136. @d n_missing = 18    {\.{missing\$}}
  6137. @d n_newline = 19    {\.{newline\$}}
  6138. @d n_num_names = 20    {\.{num.names\$}}
  6139. @d n_pop = 21        {\.{pop\$}}
  6140. @d n_preamble = 22    {\.{preamble\$}}
  6141. @d n_purify = 23    {\.{purify\$}}
  6142. @d n_quote = 24        {\.{quote\$}}
  6143. @d n_skip = 25        {\.{skip\$}}
  6144. @d n_stack = 26        {\.{stack\$}}
  6145. @d n_substring = 27    {\.{substring\$}}
  6146. @d n_swap = 28        {\.{swap\$}}
  6147. @d n_text_length = 29    {\.{text.length\$}}
  6148. @d n_text_prefix = 30    {\.{text.prefix\$}}
  6149. @d n_top_stack = 31    {\.{top\$}}
  6150. @d n_type = 32        {\.{type\$}}
  6151. @d n_warning = 33    {\.{warning\$}}
  6152. @d n_while = 34        {\.{while\$}}
  6153. @d n_width = 35        {\.{width\$}}
  6154. @d n_write = 36        {\.{write\$}}
  6155. @<Constants in the outer block@>=
  6156. @!num_blt_in_fns = 37;    {one more than the previous number}
  6157. @^add a built-in function@>
  6158. @^important note@>
  6159. It's time for us to insert more pre-defined strings into |str_pool|
  6160. (and thus the hash table) and to insert the |built_in| functions into
  6161. the hash table.  The strings corresponding to these functions should
  6162. contain no upper-case letters, and they must all be exactly
  6163. |longest_pds| characters long.  The |build_in| routine (to appear
  6164. shortly) does the work.
  6165. Important note: These pre-definitions must not have any glitches or the
  6166. program may bomb because the |log_file| hasn't been opened yet.
  6167. @<Pre-define certain strings@>=
  6168. build_in('=           ',1,b_equals,n_equals);
  6169. build_in('>           ',1,b_greater_than,n_greater_than);
  6170. build_in('<           ',1,b_less_than,n_less_than);
  6171. build_in('+           ',1,b_plus,n_plus);
  6172. build_in('-           ',1,b_minus,n_minus);
  6173. build_in('*           ',1,b_concatenate,n_concatenate);
  6174. build_in(':=          ',2,b_gets,n_gets);
  6175. build_in('add.period$ ',11,b_add_period,n_add_period);
  6176. build_in('call.type$  ',10,b_call_type,n_call_type);
  6177. build_in('change.case$',12,b_change_case,n_change_case);
  6178. build_in('chr.to.int$ ',11,b_chr_to_int,n_chr_to_int);
  6179. build_in('cite$       ',5,b_cite,n_cite);
  6180. build_in('duplicate$  ',10,b_duplicate,n_duplicate);
  6181. build_in('empty$      ',6,b_empty,n_empty);
  6182. build_in('format.name$',12,b_format_name,n_format_name);
  6183. build_in('if$         ',3,b_if,n_if);
  6184. build_in('int.to.chr$ ',11,b_int_to_chr,n_int_to_chr);
  6185. build_in('int.to.str$ ',11,b_int_to_str,n_int_to_str);
  6186. build_in('missing$    ',8,b_missing,n_missing);
  6187. build_in('newline$    ',8,b_newline,n_newline);
  6188. build_in('num.names$  ',10,b_num_names,n_num_names);
  6189. build_in('pop$        ',4,b_pop,n_pop);
  6190. build_in('preamble$   ',9,b_preamble,n_preamble);
  6191. build_in('purify$     ',7,b_purify,n_purify);
  6192. build_in('quote$      ',6,b_quote,n_quote);
  6193. build_in('skip$       ',5,b_skip,n_skip);
  6194. build_in('stack$      ',6,b_stack,n_stack);
  6195. build_in('substring$  ',10,b_substring,n_substring);
  6196. build_in('swap$       ',5,b_swap,n_swap);
  6197. build_in('text.length$',12,b_text_length,n_text_length);
  6198. build_in('text.prefix$',12,b_text_prefix,n_text_prefix);
  6199. build_in('top$        ',4,b_top_stack,n_top_stack);
  6200. build_in('type$       ',5,b_type,n_type);
  6201. build_in('warning$    ',8,b_warning,n_warning);
  6202. build_in('width$      ',6,b_width,n_width);
  6203. build_in('while$      ',6,b_while,n_while);
  6204. build_in('width$      ',6,b_width,n_width);
  6205. build_in('write$      ',6,b_write,n_write);
  6206. This procedure inserts a |built_in| function into the hash table and
  6207. initializes the corresponding pre-defined string (of length at most
  6208. |longest_pds|).  The array |fn_info| contains a number from 0 through
  6209. the number of |built_in| functions minus 1 (i.e., |num_blt_in_fns - 1|
  6210. if we're keeping statistics); this number is used by a |case|
  6211. statement to execute this function and is used for keeping execution
  6212. counts when keeping statistics.
  6213. @<Procedures and functions for handling numbers, characters, and strings@>=
  6214. procedure build_in (@!pds:pds_type; @!len:pds_len; var fn_hash_loc:hash_loc;
  6215.                     @!blt_in_num:blt_in_range);
  6216. begin
  6217. pre_define (pds,len,bst_fn_ilk);@/
  6218. fn_hash_loc := pre_def_loc;    {the |pre_define| routine sets |pre_def_loc|}
  6219. fn_type[fn_hash_loc] := built_in;
  6220. fn_info[fn_hash_loc] := blt_in_num;
  6221.   stat
  6222.   blt_in_loc[blt_in_num] := fn_hash_loc;@/
  6223.   execution_count[blt_in_num] := 0; {initialize the function-execution count}
  6224.   tats@/
  6225. This is a procedure so that |initialize| is smaller.
  6226. @<Procedures and functions for handling numbers, characters, and strings@>=
  6227. procedure pre_def_certain_strings;
  6228. begin
  6229. @<Pre-define certain strings@>@;
  6230. These variables all begin with |s_| and specify the locations in
  6231. |str_pool| of certain often-used strings that the \.{.bst} commands
  6232. need.  The |s_preamble| array is big enough to allow an average of one
  6233. \.{preamble\$} command per \.{.bib} file.
  6234. @<Globals in the outer block@>=
  6235. @!s_null : str_number;        {the null string}
  6236. @!s_default : str_number;    {\.{default.type}, for unknown entry types}
  6237. @!s_t : str_number;        {\.{t}, for |title_lowers| case conversion}
  6238. @!s_l : str_number;        {\.{l}, for |all_lowers| case conversion}
  6239. @!s_u : str_number;        {\.{u}, for |all_uppers| case conversion}
  6240. @!s_preamble : array[bib_number] of str_number;
  6241.                 {for the \.{preamble\$} |built_in| function}
  6242. These constants all begin with |n_| and are used for the |case|
  6243. statement that determines which, if any, control sequence we're
  6244. dealing with; a control sequence of interest will be either one of the
  6245. undotted characters `\.{\\i}' or `\.{\\j}' or one of the foreign
  6246. characters in Table~3.2 of the \LaTeX\ manual.
  6247. @d n_i = 0        {\.{i}, for the undotted character \.{\\i}}
  6248. @d n_j = 1        {\.{j}, for the undotted character \.{\\j}}
  6249. @d n_oe = 2        {\.{oe}, for the foreign character \.{\\oe}}
  6250. @d n_oe_upper = 3    {\.{OE}, for the foreign character \.{\\OE}}
  6251. @d n_ae = 4        {\.{ae}, for the foreign character \.{\\ae}}
  6252. @d n_ae_upper = 5    {\.{AE}, for the foreign character \.{\\AE}}
  6253. @d n_aa = 6        {\.{aa}, for the foreign character \.{\\aa}}
  6254. @d n_aa_upper = 7    {\.{AA}, for the foreign character \.{\\AA}}
  6255. @d n_o = 8        {\.{o}, for the foreign character \.{\\o}}
  6256. @d n_o_upper = 9    {\.{O}, for the foreign character \.{\\O}}
  6257. @d n_l = 10        {\.{l}, for the foreign character \.{\\l}}
  6258. @d n_l_upper = 11    {\.{L}, for the foreign character \.{\\L}}
  6259. @d n_ss = 12        {\.{ss}, for the foreign character \.{\\ss}}
  6260. @^important note@>
  6261. @.default.type@>
  6262. Here we pre-define a few strings used in executing the \.{.bst} file:
  6263. the null string, which is sometimes pushed onto the stack; a string
  6264. used for default entry types; and some control sequences used to spot
  6265. foreign characters.  We also initialize the |s_preamble| array to
  6266. empty.  These pre-defined strings must all be exactly |longest_pds|
  6267. characters long.
  6268. Important note: These pre-definitions must not have any glitches or
  6269. the program may bomb because the |log_file| hasn't been opened yet,
  6270. and |text_ilk|s should be pre-defined here, not earlier, for
  6271. \.{.bst}-function-execution purposes.
  6272. @<Pre-define certain strings@>=
  6273. pre_define('            ',0,text_ilk);    s_null := hash_text[pre_def_loc];
  6274. fn_type[pre_def_loc] := str_literal;@/
  6275. pre_define('default.type',12,text_ilk);    s_default := hash_text[pre_def_loc];
  6276. fn_type[pre_def_loc] := str_literal;@/
  6277. b_default := b_skip;    {this may be changed to the \.{default.type} function}
  6278. preamble_ptr := 0;            {initialize the |s_preamble| array}
  6279. pre_define('i           ',1,control_seq_ilk);
  6280. ilk_info[pre_def_loc] := n_i;
  6281. pre_define('j           ',1,control_seq_ilk);
  6282. ilk_info[pre_def_loc] := n_j;
  6283. pre_define('oe          ',2,control_seq_ilk);
  6284. ilk_info[pre_def_loc] := n_oe;
  6285. pre_define('OE          ',2,control_seq_ilk);
  6286. ilk_info[pre_def_loc] := n_oe_upper;
  6287. pre_define('ae          ',2,control_seq_ilk);
  6288. ilk_info[pre_def_loc] := n_ae;
  6289. pre_define('AE          ',2,control_seq_ilk);
  6290. ilk_info[pre_def_loc] := n_ae_upper;
  6291. pre_define('aa          ',2,control_seq_ilk);
  6292. ilk_info[pre_def_loc] := n_aa;
  6293. pre_define('AA          ',2,control_seq_ilk);
  6294. ilk_info[pre_def_loc] := n_aa_upper;
  6295. pre_define('o           ',1,control_seq_ilk);
  6296. ilk_info[pre_def_loc] := n_o;
  6297. pre_define('O           ',1,control_seq_ilk);
  6298. ilk_info[pre_def_loc] := n_o_upper;
  6299. pre_define('l           ',1,control_seq_ilk);
  6300. ilk_info[pre_def_loc] := n_l;
  6301. pre_define('L           ',1,control_seq_ilk);
  6302. ilk_info[pre_def_loc] := n_l_upper;
  6303. pre_define('ss          ',2,control_seq_ilk);
  6304. ilk_info[pre_def_loc] := n_ss;
  6305. @^important note@>
  6306. @.crossref@>
  6307. @.entry.max\$@>
  6308. @.global.max\$@>
  6309. @.sort.key\$@>
  6310. Now we pre-define any built-in |field|s, |str_entry_var|s, and
  6311. |int_global_var|s; these strings must all be exactly |longest_pds|
  6312. characters long.  Note that although these are built-in functions, we
  6313. classify them (in the |fn_type| array) otherwise.
  6314. Important note: These pre-definitions must not have any glitches or
  6315. the program may bomb because the |log_file| hasn't been opened yet.
  6316. @<Pre-define certain strings@>=
  6317. pre_define('crossref    ',8,bst_fn_ilk);
  6318. fn_type[pre_def_loc] := field;@/
  6319. fn_info[pre_def_loc] := num_fields;    {give this |field| a number}
  6320. crossref_num := num_fields;
  6321. incr(num_fields);@/
  6322. num_pre_defined_fields := num_fields;    {that's it for pre-defined |field|s}
  6323. pre_define('sort.key$   ',9,bst_fn_ilk);
  6324. fn_type[pre_def_loc] := str_entry_var;
  6325. fn_info[pre_def_loc] := num_ent_strs;    {give this |str_entry_var| a number}
  6326. sort_key_num := num_ent_strs;
  6327. incr(num_ent_strs);@/
  6328. pre_define('entry.max$  ',10,bst_fn_ilk);
  6329. fn_type[pre_def_loc] := int_global_var;
  6330. fn_info[pre_def_loc] := ent_str_size;    {initialize this |int_global_var|}
  6331. pre_define('global.max$ ',11,bst_fn_ilk);
  6332. fn_type[pre_def_loc] := int_global_var;
  6333. fn_info[pre_def_loc] := glob_str_size;    {initialize this |int_global_var|}
  6334. @^add a built-in function@>
  6335. @:this can't happen}{\quad Unknown built-in function@>
  6336. This module branches to the code for the appropriate |built_in|
  6337. function.  Only three---{\.{call.type\$}}, {\.{if\$}}, and
  6338. {\.{while\$}}---do a recursive call.
  6339. @<Execute a |built_in| function@>=
  6340. begin
  6341.   stat        {update this function's execution count}
  6342.   incr(execution_count[fn_info[ex_fn_loc]]);
  6343.   tats@/
  6344. case (fn_info[ex_fn_loc]) of
  6345.     n_equals :        x_equals;
  6346.     n_greater_than :    x_greater_than;
  6347.     n_less_than :    x_less_than;
  6348.     n_plus :        x_plus;
  6349.     n_minus :        x_minus;
  6350.     n_concatenate :    x_concatenate;
  6351.     n_gets :        x_gets;
  6352.     n_add_period :    x_add_period;
  6353.     n_call_type :    @<|execute_fn|({\.{call.type\$}})@>;
  6354.     n_change_case :    x_change_case;
  6355.     n_chr_to_int :    x_chr_to_int;
  6356.     n_cite :        x_cite;
  6357.     n_duplicate :    x_duplicate;
  6358.     n_empty :        x_empty;
  6359.     n_format_name :    x_format_name;
  6360.     n_if :        @<|execute_fn|({\.{if\$}})@>;
  6361.     n_int_to_chr :    x_int_to_chr;
  6362.     n_int_to_str :    x_int_to_str;
  6363.     n_missing :        x_missing;
  6364.     n_newline :        @<|execute_fn|({\.{newline\$}})@>;
  6365.     n_num_names :    x_num_names;
  6366.     n_pop :        @<|execute_fn|({\.{pop\$}})@>;
  6367.     n_preamble :    x_preamble;
  6368.     n_purify :        x_purify;
  6369.     n_quote :        x_quote;
  6370.     n_skip :        @<|execute_fn|({\.{skip\$}})@>;
  6371.     n_stack :        @<|execute_fn|({\.{stack\$}})@>;
  6372.     n_substring :    x_substring;
  6373.     n_swap :        x_swap;
  6374.     n_text_length :    x_text_length;
  6375.     n_text_prefix :    x_text_prefix;
  6376.     n_top_stack :    @<|execute_fn|({\.{top\$}})@>;
  6377.     n_type :        x_type;
  6378.     n_warning :        x_warning;
  6379.     n_while :        @<|execute_fn|({\.{while\$}})@>;
  6380.     n_width :        x_width;
  6381.     n_write :        x_write;
  6382.     othercases confusion ('Unknown built-in function')
  6383. endcases;
  6384. @^add a built-in function@>
  6385. @^gymnastics@>
  6386. This extra level of module-pointing allows a uniformity of module
  6387. names for the |built_in| functions, regardless of whether they do a
  6388. recursive call to |execute_fn| or are trivial (a single statement).
  6389. Those that do a recursive call are left as part of |execute_fn|,
  6390. avoiding \PASCAL's forward procedure mechanism, and those that don't
  6391. (except for the single-statement ones) are made into procedures so
  6392. that |execute_fn| doesn't get too large.
  6393. @<Procedures and functions for style-file function execution@>=
  6394. @<|execute_fn|({\.{=}})@>@;
  6395. @<|execute_fn|({\.{>}})@>@;
  6396. @<|execute_fn|({\.{<}})@>@;
  6397. @<|execute_fn|({\.{+}})@>@;
  6398. @<|execute_fn|({\.{-}})@>@;
  6399. @<|execute_fn|({\.{*}})@>@;
  6400. @<|execute_fn|({\.{:=}})@>@;
  6401. @<|execute_fn|({\.{add.period\$}})@>@;
  6402. @<|execute_fn|({\.{change.case\$}})@>@;
  6403. @<|execute_fn|({\.{chr.to.int\$}})@>@;
  6404. @<|execute_fn|({\.{cite\$}})@>@;
  6405. @<|execute_fn|({\.{duplicate\$}})@>@;
  6406. @<|execute_fn|({\.{empty\$}})@>@;
  6407. @<|execute_fn|({\.{format.name\$}})@>@;
  6408. @<|execute_fn|({\.{int.to.chr\$}})@>@;
  6409. @<|execute_fn|({\.{int.to.str\$}})@>@;
  6410. @<|execute_fn|({\.{missing\$}})@>@;
  6411. @<|execute_fn|({\.{num.names\$}})@>@;
  6412. @<|execute_fn|({\.{preamble\$}})@>@;
  6413. @<|execute_fn|({\.{purify\$}})@>@;
  6414. @<|execute_fn|({\.{quote\$}})@>@;
  6415. @<|execute_fn|({\.{substring\$}})@>@;
  6416. @<|execute_fn|({\.{swap\$}})@>@;
  6417. @<|execute_fn|({\.{text.length\$}})@>@;
  6418. @<|execute_fn|({\.{text.prefix\$}})@>@;
  6419. @<|execute_fn|({\.{type\$}})@>@;
  6420. @<|execute_fn|({\.{warning\$}})@>@;
  6421. @<|execute_fn|({\.{width\$}})@>@;
  6422. @<|execute_fn|({\.{write\$}})@>@;
  6423. @<|execute_fn| itself@>
  6424. Now it's time to declare some things for executing |built_in|
  6425. functions only.  These (and only these) variables are used
  6426. recursively, so they can't be global.
  6427. @d end_while = 51    {stop executing the \.{while\$} function}
  6428. @<Declarations for executing |built_in| functions@>=
  6429. label end_while;
  6430. var r_pop_lt1,@!r_pop_lt2 : integer;    {stack literals for \.{while\$}}
  6431. @!r_pop_tp1,@!r_pop_tp2 : stk_type;    {stack types for \.{while\$}}
  6432. These are nonrecursive variables that |execute_fn| uses.  Declaring
  6433. them here (instead of in the previous module) saves execution time and
  6434. stack space on most machines.
  6435. @d name_buf == sv_buffer    {an alias, a buffer for manipulating names}
  6436. @<Globals in the outer block@>=
  6437. @!pop_lit1,@!pop_lit2,@!pop_lit3 : integer;    {stack literals}
  6438. @!pop_typ1,@!pop_typ2,@!pop_typ3 : stk_type;    {stack types}
  6439. @!sp_ptr : pool_pointer;        {for manipulating |str_pool| strings}
  6440. @!sp_xptr1,@!sp_xptr2 : pool_pointer;    {more of the same}
  6441. @!sp_end : pool_pointer;        {marks the end of a |str_pool| string}
  6442. @!sp_length,sp2_length : pool_pointer;    {lengths of |str_pool| strings}
  6443. @!sp_brace_level : integer;        {for scanning |str_pool| strings}
  6444. @!ex_buf_xptr,@!ex_buf_yptr : buf_pointer;    {extra |ex_buf| locations}
  6445. @!control_seq_loc : hash_loc;    {hash-table loc of a control sequence}
  6446. @!preceding_white : boolean;    {used in scanning strings}
  6447. @!and_found : boolean;        {to stop the loop that looks for an ``and''}
  6448. @!num_names : integer;        {for counting names}
  6449. @!name_bf_ptr : buf_pointer;    {general |name_buf| location}
  6450. @!name_bf_xptr,@!name_bf_yptr : buf_pointer;    {and two more}
  6451. @!nm_brace_level : integer;    {for scanning |name_buf| strings}
  6452. @!name_tok : packed array[buf_pointer] of buf_pointer; {name-token ptr list}
  6453. @!name_sep_char : packed array[buf_pointer] of ASCII_code; {token-ending chars}
  6454. @!num_tokens : buf_pointer;    {this counts name tokens}
  6455. @!token_starting : boolean;    {used in scanning name tokens}
  6456. @!alpha_found : boolean;    {used in scanning the format string}
  6457. @!double_letter,@!end_of_group,@!to_be_written : boolean;    {the same}
  6458. @!first_start : buf_pointer;    {start-ptr into |name_tok| for the first name}
  6459. @!first_end : buf_pointer;    {end-ptr into |name_tok| for the first name}
  6460. @!last_end : buf_pointer;    {end-ptr into |name_tok| for the last name}
  6461. @!von_start : buf_pointer;    {start-ptr into |name_tok| for the von name}
  6462. @!von_end : buf_pointer;    {end-ptr into |name_tok| for the von name}
  6463. @!jr_end : buf_pointer;        {end-ptr into |name_tok| for the jr name}
  6464. @!cur_token,@!last_token : buf_pointer;    {|name_tok| ptrs for outputting tokens}
  6465. @!use_default : boolean;    {for the inter-token intra-name part string}
  6466. @!num_commas : buf_pointer;    {used to determine the name syntax}
  6467. @!comma1,@!comma2 : buf_pointer;    {ptrs into |name_tok|}
  6468. @!num_text_chars : buf_pointer;    {special characters count as one}
  6469. The |built_in| function {\.{=}} pops the top two (integer or string)
  6470. literals, compares them, and pushes the integer 1 if they're equal, 0
  6471. otherwise.  If they're not either both string or both integer, it
  6472. complains and pushes the integer 0.
  6473. @<|execute_fn|({\.{=}})@>=
  6474. procedure x_equals;
  6475. begin
  6476. pop_lit_stk (pop_lit1,pop_typ1);
  6477. pop_lit_stk (pop_lit2,pop_typ2);
  6478. if (pop_typ1 <> pop_typ2) then
  6479.     begin
  6480.     if ((pop_typ1 <> stk_empty) and (pop_typ2 <> stk_empty)) then
  6481.     begin
  6482.     print_stk_lit (pop_lit1,pop_typ1);
  6483.     print (', ');
  6484.     print_stk_lit (pop_lit2,pop_typ2);
  6485.     print_newline;
  6486.     bst_ex_warn ('---they aren''t the same literal types');
  6487.     end;
  6488.     push_lit_stk (0, stk_int);
  6489.     end
  6490. else if ((pop_typ1 <> stk_int) and (pop_typ1 <> stk_str)) then
  6491.     begin
  6492.     if (pop_typ1 <> stk_empty) then
  6493.     begin
  6494.     print_stk_lit (pop_lit1,pop_typ1);
  6495.     bst_ex_warn (', not an integer or a string,');
  6496.     end;
  6497.     push_lit_stk (0, stk_int);
  6498.     end
  6499. else if (pop_typ1 = stk_int) then
  6500.     if (pop_lit2 = pop_lit1) then
  6501.     push_lit_stk (1, stk_int)
  6502.       else
  6503.     push_lit_stk (0, stk_int)
  6504.     if (str_eq_str (pop_lit2,pop_lit1)) then
  6505.     push_lit_stk (1, stk_int)
  6506.       else
  6507.     push_lit_stk (0, stk_int);
  6508. The |built_in| function {\.{>}} pops the top two (integer) literals,
  6509. compares them, and pushes the integer 1 if the second is greater than
  6510. the first, 0 otherwise.  If either isn't an integer literal, it
  6511. complains and pushes the integer 0.
  6512. @<|execute_fn|({\.{>}})@>=
  6513. procedure x_greater_than;
  6514. begin
  6515. pop_lit_stk (pop_lit1,pop_typ1);
  6516. pop_lit_stk (pop_lit2,pop_typ2);
  6517. if (pop_typ1 <> stk_int) then
  6518.     begin
  6519.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  6520.     push_lit_stk (0, stk_int);
  6521.     end
  6522. else if (pop_typ2 <> stk_int) then
  6523.     begin
  6524.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  6525.     push_lit_stk (0, stk_int);
  6526.     end
  6527.     if (pop_lit2 > pop_lit1) then
  6528.     push_lit_stk (1, stk_int)
  6529.       else
  6530.     push_lit_stk (0, stk_int);
  6531. The |built_in| function {\.{<}} pops the top two (integer) literals,
  6532. compares them, and pushes the integer 1 if the second is less than the
  6533. first, 0 otherwise.  If either isn't an integer literal, it complains
  6534. and pushes the integer 0.
  6535. @<|execute_fn|({\.{<}})@>=
  6536. procedure x_less_than;
  6537. begin
  6538. pop_lit_stk (pop_lit1,pop_typ1);
  6539. pop_lit_stk (pop_lit2,pop_typ2);
  6540. if (pop_typ1 <> stk_int) then
  6541.     begin
  6542.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  6543.     push_lit_stk (0, stk_int);
  6544.     end
  6545. else if (pop_typ2 <> stk_int) then
  6546.     begin
  6547.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  6548.     push_lit_stk (0, stk_int);
  6549.     end
  6550.     if (pop_lit2 < pop_lit1) then
  6551.     push_lit_stk (1, stk_int)
  6552.       else
  6553.     push_lit_stk (0, stk_int);
  6554. The |built_in| function {\.{+}} pops the top two (integer) literals
  6555. and pushes their sum.  If either isn't an integer literal, it
  6556. complains and pushes the integer 0.
  6557. @<|execute_fn|({\.{+}})@>=
  6558. procedure x_plus;
  6559. begin
  6560. pop_lit_stk (pop_lit1,pop_typ1);
  6561. pop_lit_stk (pop_lit2,pop_typ2);
  6562. if (pop_typ1 <> stk_int) then
  6563.     begin
  6564.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  6565.     push_lit_stk (0, stk_int);
  6566.     end
  6567. else if (pop_typ2 <> stk_int) then
  6568.     begin
  6569.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  6570.     push_lit_stk (0, stk_int);
  6571.     end
  6572.     push_lit_stk (pop_lit2+pop_lit1, stk_int);
  6573. The |built_in| function {\.{-}} pops the top two (integer) literals
  6574. and pushes their difference (the first subtracted from the second).
  6575. If either isn't an integer literal, it complains and pushes the
  6576. integer 0.
  6577. @<|execute_fn|({\.{-}})@>=
  6578. procedure x_minus;
  6579. begin
  6580. pop_lit_stk (pop_lit1,pop_typ1);
  6581. pop_lit_stk (pop_lit2,pop_typ2);
  6582. if (pop_typ1 <> stk_int) then
  6583.     begin
  6584.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  6585.     push_lit_stk (0, stk_int);
  6586.     end
  6587. else if (pop_typ2 <> stk_int) then
  6588.     begin
  6589.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  6590.     push_lit_stk (0, stk_int);
  6591.     end
  6592.     push_lit_stk (pop_lit2-pop_lit1, stk_int);
  6593. The |built_in| function {\.{*}} pops the top two (string) literals,
  6594. concatenates them (in reverse order, that is, the order in which
  6595. pushed), and pushes the resulting string back onto the stack.  If
  6596. either isn't a string literal, it complains and pushes the null
  6597. string.
  6598. @<|execute_fn|({\.{*}})@>=
  6599. procedure x_concatenate;
  6600. begin
  6601. pop_lit_stk (pop_lit1,pop_typ1);
  6602. pop_lit_stk (pop_lit2,pop_typ2);
  6603. if (pop_typ1 <> stk_str) then
  6604.     begin
  6605.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  6606.     push_lit_stk (s_null, stk_str);
  6607.     end
  6608. else if (pop_typ2 <> stk_str) then
  6609.     begin
  6610.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
  6611.     push_lit_stk (s_null, stk_str);
  6612.     end
  6613.     @<Concatenate the two strings and push@>;
  6614. @^push the literal stack@>
  6615. Often both strings will be at the top of the string pool, in which
  6616. case we just move some pointers.  Furthermore, it's worth doing some
  6617. special stuff in case either string is null, since empirically this
  6618. seems to happen about $20\%$ of the time.  In any case, we don't need
  6619. the execution buffer---we simple move the strings around in the string
  6620. pool when necessary.
  6621. @<Concatenate the two strings and push@>=
  6622. begin
  6623. if (pop_lit2 >= cmd_str_ptr) then
  6624.     if (pop_lit1 >= cmd_str_ptr) then
  6625.     begin
  6626.     str_start[pop_lit1] := str_start[pop_lit1+1];
  6627.     unflush_string;
  6628.     incr(lit_stk_ptr);
  6629.     else if (length(pop_lit2) = 0) then
  6630.     push_lit_stk (pop_lit1, stk_str)
  6631.     else    {|pop_lit2| is nonnull, only |pop_lit1| is below |cmd_str_ptr|}
  6632.     begin
  6633.     pool_ptr := str_start[pop_lit2+1];
  6634.     str_room (length(pop_lit1));
  6635.     sp_ptr := str_start[pop_lit1];
  6636.     sp_end := str_start[pop_lit1+1];
  6637.     while (sp_ptr < sp_end) do
  6638.         begin
  6639.         append_char (str_pool[sp_ptr]);
  6640.         incr(sp_ptr);
  6641.         end;
  6642.     push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  6643.     @<Concatenate them and push when |pop_lit2 < cmd_str_ptr|@>;
  6644. @^push the literal stack@>
  6645. We simply continue the previous module.
  6646. @<Concatenate them and push when |pop_lit2 < cmd_str_ptr|@>=
  6647. begin
  6648. if (pop_lit1 >= cmd_str_ptr) then
  6649.     if (length(pop_lit2) = 0) then
  6650.     begin
  6651.     unflush_string;
  6652.     lit_stack[lit_stk_ptr] := pop_lit1;
  6653.     incr(lit_stk_ptr);
  6654.     else if (length(pop_lit1) = 0) then
  6655.     incr(lit_stk_ptr)
  6656.     else    {both strings nonnull, only |pop_lit2| is below |cmd_str_ptr|}
  6657.     begin
  6658.     sp_length := length(pop_lit1);
  6659.     sp2_length := length(pop_lit2);
  6660.     str_room (sp_length + sp2_length);
  6661.     sp_ptr := str_start[pop_lit1+1];
  6662.     sp_end := str_start[pop_lit1];
  6663.     sp_xptr1 := sp_ptr + sp2_length;
  6664.     while (sp_ptr > sp_end) do        {slide up |pop_lit1|}
  6665.         begin
  6666.         decr(sp_ptr);
  6667.         decr(sp_xptr1);
  6668.         str_pool[sp_xptr1] := str_pool[sp_ptr];
  6669.         end;
  6670.     sp_ptr := str_start[pop_lit2];
  6671.     sp_end := str_start[pop_lit2+1];
  6672.     while (sp_ptr < sp_end) do        {slide up |pop_lit2|}
  6673.         begin
  6674.         append_char (str_pool[sp_ptr]);
  6675.         incr(sp_ptr);
  6676.         end;
  6677.     pool_ptr := pool_ptr + sp_length;
  6678.     push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  6679.     @<Concatenate them and push when |pop_lit1,pop_lit2 < cmd_str_ptr|@>;
  6680. @^push the literal stack@>
  6681. Again, we simply continue the previous module.
  6682. @<Concatenate them and push when |pop_lit1,pop_lit2 < cmd_str_ptr|@>=
  6683. begin
  6684. if (length(pop_lit1) = 0) then
  6685.     incr(lit_stk_ptr)
  6686. else if (length(pop_lit2) = 0) then
  6687.     push_lit_stk (pop_lit1, stk_str)
  6688. else        {both strings are nonnull, and both are below |cmd_str_ptr|}
  6689.     begin
  6690.     str_room (length(pop_lit1) + length(pop_lit2));
  6691.     sp_ptr := str_start[pop_lit2];
  6692.     sp_end := str_start[pop_lit2+1];
  6693.     while (sp_ptr < sp_end) do            {slide up |pop_lit2|}
  6694.     begin
  6695.     append_char (str_pool[sp_ptr]);
  6696.     incr(sp_ptr);
  6697.     end;
  6698.     sp_ptr := str_start[pop_lit1];
  6699.     sp_end := str_start[pop_lit1+1];
  6700.     while (sp_ptr < sp_end) do            {slide up |pop_lit1|}
  6701.     begin
  6702.     append_char (str_pool[sp_ptr]);
  6703.     incr(sp_ptr);
  6704.     end;
  6705.     push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  6706.     end;
  6707. The |built_in| function {\.{:=}} pops the top two literals and assigns
  6708. to the first (which must be an |int_entry_var|, a |str_entry_var|, an
  6709. |int_global_var|, or a |str_global_var|) the value of the second;
  6710. it complains if the value isn't of the appropriate type.
  6711. @<|execute_fn|({\.{:=}})@>=
  6712. procedure x_gets;
  6713. begin
  6714. pop_lit_stk (pop_lit1,pop_typ1);
  6715. pop_lit_stk (pop_lit2,pop_typ2);
  6716. if (pop_typ1 <> stk_fn) then
  6717.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_fn)
  6718. else if ((not mess_with_entries) and
  6719.     ((fn_type[pop_lit1] = str_entry_var) or
  6720.      (fn_type[pop_lit1] = int_entry_var))) then
  6721.     bst_cant_mess_with_entries_print
  6722.     case (fn_type[pop_lit1]) of
  6723.     int_entry_var :    @<Assign to an |int_entry_var|@>;
  6724.     str_entry_var :    @<Assign to a |str_entry_var|@>;
  6725.     int_global_var : @<Assign to an |int_global_var|@>;
  6726.     str_global_var : @<Assign to a |str_global_var|@>;
  6727.     othercases begin
  6728.            print ('You can''t assign to type ');
  6729.            print_fn_class (pop_lit1);
  6730.            bst_ex_warn (', a nonvariable function class');
  6731.            end
  6732.     endcases;
  6733. This module checks that what we're about to assign is really an
  6734. integer, and then assigns.
  6735. @<Assign to an |int_entry_var|@>=
  6736. if (pop_typ2 <> stk_int) then
  6737.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int)
  6738.   else
  6739.     entry_ints[cite_ptr*num_ent_ints+fn_info[pop_lit1]] := pop_lit2
  6740. @.String size exceeded@>
  6741. It's time for a complaint if either of the two (entry or global)
  6742. string lengths is exceeded.
  6743. @d bst_string_size_exceeded(#) == begin
  6744.                   bst_1print_string_size_exceeded;
  6745.                   print (#);
  6746.                   bst_2print_string_size_exceeded;
  6747.                   end
  6748. @<Procedures and functions for all file I/O, error messages, and such@>=
  6749. procedure bst_1print_string_size_exceeded;
  6750. begin
  6751. print ('Warning--you''ve exceeded ');
  6752. procedure bst_2print_string_size_exceeded;
  6753. begin
  6754. print ('-string-size,');
  6755. bst_mild_ex_warn_print;
  6756. print_ln ('*Please notify the bibstyle designer*');
  6757. @.entry string size exceeded@>
  6758. @:String size exceeded}{\quad entry string size@>
  6759. This module checks that what we're about to assign is really a
  6760. string, and then assigns.
  6761. @<Assign to a |str_entry_var|@>=
  6762. begin
  6763. if (pop_typ2 <> stk_str) then
  6764.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str)
  6765.   else
  6766.     begin
  6767.     str_ent_ptr := cite_ptr*num_ent_strs + fn_info[pop_lit1];
  6768.     ent_chr_ptr := 0;
  6769.     sp_ptr := str_start[pop_lit2];
  6770.     sp_xptr1 := str_start[pop_lit2+1];
  6771.     if (sp_xptr1-sp_ptr > ent_str_size) then
  6772.     begin
  6773.     bst_string_size_exceeded (ent_str_size:0,', the entry');
  6774.     sp_xptr1 := sp_ptr + ent_str_size;
  6775.     end;
  6776.     while (sp_ptr < sp_xptr1) do
  6777.     begin            {copy characters into |entry_strs|}
  6778.     entry_strs[str_ent_ptr][ent_chr_ptr] := str_pool[sp_ptr];
  6779.     incr(ent_chr_ptr);
  6780.     incr(sp_ptr);
  6781.     end;
  6782.     entry_strs[str_ent_ptr][ent_chr_ptr] := end_of_string;
  6783.     end
  6784. This module checks that what we're about to assign is really an
  6785. integer, and then assigns.
  6786. @<Assign to an |int_global_var|@>=
  6787. if (pop_typ2 <> stk_int) then
  6788.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int)
  6789.   else
  6790.     fn_info[pop_lit1] := pop_lit2
  6791. @.global string size exceeded@>
  6792. @:String size exceeded}{\quad global string size@>
  6793. This module checks that what we're about to assign is really a
  6794. string, and then assigns.
  6795. @<Assign to a |str_global_var|@>=
  6796. begin
  6797. if (pop_typ2 <> stk_str) then
  6798.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str)
  6799.   else
  6800.     begin
  6801.     str_glb_ptr := fn_info[pop_lit1];
  6802.     if (pop_lit2 < cmd_str_ptr) then
  6803.     glb_str_ptr[str_glb_ptr] := pop_lit2
  6804.       else
  6805.     begin
  6806.     glb_str_ptr[str_glb_ptr] := 0;
  6807.     glob_chr_ptr := 0;
  6808.     sp_ptr := str_start[pop_lit2];
  6809.     sp_end := str_start[pop_lit2+1];
  6810.     if (sp_end - sp_ptr > glob_str_size) then
  6811.         begin
  6812.         bst_string_size_exceeded (glob_str_size:0,', the global');
  6813.         sp_end := sp_ptr + glob_str_size;
  6814.         end;
  6815.     while (sp_ptr < sp_end) do
  6816.         begin            {copy characters into |global_strs|}
  6817.         global_strs[str_glb_ptr][glob_chr_ptr] := str_pool[sp_ptr];
  6818.         incr(glob_chr_ptr);
  6819.         incr(sp_ptr);
  6820.         end;
  6821.     glb_str_end[str_glb_ptr] := glob_chr_ptr;
  6822.     end;
  6823.     end
  6824. The |built_in| function {\.{add.period\$}} pops the top (string)
  6825. literal, adds a |period| to a nonnull string if its last
  6826. non|right_brace| character isn't a |period|, |question_mark|, or
  6827. |exclamation_mark|, and pushes this resulting string back onto the
  6828. stack.  If the literal isn't a string, it complains and pushes the
  6829. null string.
  6830. @<|execute_fn|({\.{add.period\$}})@>=
  6831. procedure x_add_period;
  6832. label loop_exit;
  6833. begin
  6834. pop_lit_stk (pop_lit1,pop_typ1);
  6835. if (pop_typ1 <> stk_str) then
  6836.     begin
  6837.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  6838.     push_lit_stk (s_null, stk_str);
  6839.     end
  6840. else if (length(pop_lit1) = 0) then    {don't add |period| to the null string}
  6841.     push_lit_stk (s_null, stk_str)
  6842.     @<Add the |period|, if necessary, and push@>;
  6843. @^push the literal stack@>
  6844. Here we scan backwards from the end of the string, skipping
  6845. non|right_brace| characters, to see if we have to add the |period|.
  6846. @<Add the |period|, if necessary, and push@>=
  6847. begin
  6848. sp_ptr := str_start[pop_lit1+1];
  6849. sp_end := str_start[pop_lit1];
  6850. while (sp_ptr > sp_end) do            {find a non|right_brace|}
  6851.     begin
  6852.     decr(sp_ptr);
  6853.     if (str_pool[sp_ptr] <> right_brace) then
  6854.     goto loop_exit;
  6855.     end;
  6856. loop_exit:
  6857. case (str_pool[sp_ptr]) of
  6858.     period,
  6859.     question_mark,
  6860.     exclamation_mark :
  6861.     repush_string;
  6862.     othercases
  6863.     @<Add the |period| (it's necessary) and push@>
  6864. endcases;
  6865. Ok guys, we really have to do it.
  6866. @<Add the |period| (it's necessary) and push@>=
  6867. begin
  6868. if (pop_lit1 < cmd_str_ptr) then
  6869.     begin
  6870.     str_room (length(pop_lit1)+1);
  6871.     sp_ptr := str_start[pop_lit1];
  6872.     sp_end := str_start[pop_lit1+1];
  6873.     while (sp_ptr < sp_end) do        {slide |pop_lit1| atop the string pool}
  6874.     begin
  6875.     append_char (str_pool[sp_ptr]);
  6876.     incr(sp_ptr);
  6877.     end;
  6878.     end
  6879. else                    {the string is already there}
  6880.     begin
  6881.     pool_ptr := str_start[pop_lit1+1];
  6882.     str_room (1);
  6883.     end;
  6884. append_char (period);
  6885. push_lit_stk (make_string, stk_str);
  6886. The |built_in| function {\.{call.type\$}} executes the function
  6887. specified in |type_list| for this entry unless it's |undefined|, in
  6888. which case it executes the default function \.{default.type} defined
  6889. in the \.{.bst} file, or unless it's |empty|, in which case it does
  6890. nothing.
  6891. @<|execute_fn|({\.{call.type\$}})@>=
  6892. begin
  6893. if (not mess_with_entries) then
  6894.     bst_cant_mess_with_entries_print
  6895.   else
  6896.     if (type_list[cite_ptr] = undefined) then
  6897.     execute_fn (b_default)
  6898.     else if (type_list[cite_ptr] = empty) then
  6899.     do_nothing
  6900.     else
  6901.     execute_fn (type_list[cite_ptr]);
  6902. The |built_in| function {\.{change.case\$}} pops the top two (string)
  6903. literals; it changes the case of the second according to the
  6904. specifications of the first, as follows.  (Note: The word `letters' in
  6905. the next sentence refers only to those at brace-level~0, the top-most
  6906. brace level; no other characters are changed, except perhaps for
  6907. special characters, described shortly.)  If the first literal is the
  6908. string~\.{t}, it converts to lower case all letters except the very
  6909. first character in the string, which it leaves alone, and except the
  6910. first character following any |colon| and then nonnull |white_space|,
  6911. which it also leaves alone; if it's the string~\.{l}, it converts all
  6912. letters to lower case; if it's the string~\.{u}, it converts all
  6913. letters to upper case; and if it's anything else, it complains and
  6914. does no conversion.  It then pushes this resulting string.  If either
  6915. type is incorrect, it complains and pushes the null string; however,
  6916. if both types are correct but the specification string (i.e., the
  6917. first string) isn't one of the legal ones, it merely pushes the second
  6918. back onto the stack, after complaining.  (Another note: It ignores
  6919. case differences in the specification string; for example, the strings
  6920. \.{t} and \.{T} are equivalent for the purposes of this |built_in|
  6921. function.)
  6922. @d ok_pascal_i_give_up = 21
  6923. @<|execute_fn|({\.{change.case\$}})@>=
  6924. procedure x_change_case;
  6925. label ok_pascal_i_give_up;
  6926. begin
  6927. pop_lit_stk (pop_lit1,pop_typ1);
  6928. pop_lit_stk (pop_lit2,pop_typ2);
  6929. if (pop_typ1 <> stk_str) then
  6930.     begin
  6931.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  6932.     push_lit_stk (s_null, stk_str);
  6933.     end
  6934. else if (pop_typ2 <> stk_str) then
  6935.     begin
  6936.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
  6937.     push_lit_stk (s_null, stk_str);
  6938.     end
  6939.     begin
  6940.     @<Determine the case-conversion type@>;
  6941.     ex_buf_length := 0;
  6942.     add_buf_pool (pop_lit2);
  6943.     @<Perform the case conversion@>;
  6944.     add_pool_buf_and_push;        {push this string onto the stack}
  6945.     end;
  6946. First we define a few variables for case conversion.  The constant
  6947. definitions, to be used in |case| statements, are in order of probable
  6948. frequency.
  6949. @d title_lowers = 0    {representing the string \.{t}}
  6950. @d all_lowers = 1    {representing the string \.{l}}
  6951. @d all_uppers = 2    {representing the string \.{u}}
  6952. @d bad_conversion = 3    {representing any illegal case-conversion string}
  6953. @<Globals in the outer block@>=
  6954. @!conversion_type : 0..bad_conversion;    {the possible cases}
  6955. @!prev_colon : boolean;            {|true| if just past a |colon|}
  6956. Now we determine which of the three case-conversion types we're
  6957. dealing with: \.{t},~\.{l}, or~\.{u}.
  6958. @<Determine the case-conversion type@>=
  6959. begin
  6960. case (str_pool[str_start[pop_lit1]]) of
  6961.     "t","T" : conversion_type := title_lowers;
  6962.     "l","L" : conversion_type := all_lowers;
  6963.     "u","U" : conversion_type := all_uppers;
  6964.     othercases conversion_type := bad_conversion
  6965. endcases;
  6966. if ((length(pop_lit1) <> 1) or (conversion_type = bad_conversion)) then
  6967.     begin
  6968.     conversion_type := bad_conversion;
  6969.     print_pool_str (pop_lit1);
  6970.     bst_ex_warn (' is an illegal case-conversion string');
  6971.     end;
  6972. This procedure complains if the just-encountered |right_brace| would
  6973. make |brace_level| negative.
  6974. @<Procedures and functions for name-string processing@>=
  6975. procedure decr_brace_level (@!pop_lit_var : str_number);
  6976. begin
  6977. if (brace_level = 0) then
  6978.     braces_unbalanced_complaint (pop_lit_var)
  6979.   else
  6980.     decr(brace_level);
  6981. This complaint often arises because the style designer has to type
  6982. lots of braces.
  6983. @<Procedures and functions for all file I/O, error messages, and such@>=
  6984. procedure braces_unbalanced_complaint (@!pop_lit_var : str_number);
  6985. begin
  6986. print ('Warning--"');
  6987. print_pool_str (pop_lit_var);
  6988. bst_mild_ex_warn ('" isn''t a brace-balanced string');
  6989. This one makes sure that |brace_level=0| (it's called at a point in a
  6990. string where braces must be balanced).
  6991. @<Procedures and functions for name-string processing@>=
  6992. procedure check_brace_level (@!pop_lit_var : str_number);
  6993. begin
  6994. if (brace_level > 0) then
  6995.     braces_unbalanced_complaint (pop_lit_var);
  6996. Here's where we actually go through the string and do the case
  6997. conversion.
  6998. @<Perform the case conversion@>=
  6999. begin
  7000. brace_level := 0;    {this is the top level}
  7001. ex_buf_ptr := 0;    {we start with the string's first character}
  7002. while (ex_buf_ptr < ex_buf_length) do
  7003.     begin
  7004.     if (ex_buf[ex_buf_ptr] = left_brace) then
  7005.     begin
  7006.     incr(brace_level);
  7007.     if (brace_level <> 1) then
  7008.         goto ok_pascal_i_give_up;
  7009.     if (ex_buf_ptr + 4 > ex_buf_length) then
  7010.         goto ok_pascal_i_give_up
  7011.       else if (ex_buf[ex_buf_ptr+1] <> backslash) then
  7012.         goto ok_pascal_i_give_up;
  7013.     if (conversion_type = title_lowers) then
  7014.       if (ex_buf_ptr = 0) then
  7015.         goto ok_pascal_i_give_up
  7016.       else if ((prev_colon) and
  7017.             (lex_class[ex_buf[ex_buf_ptr-1]] = white_space)) then
  7018.         goto ok_pascal_i_give_up;
  7019.     @<Convert a special character@>;
  7020. ok_pascal_i_give_up:
  7021.     prev_colon := false;
  7022.     else if (ex_buf[ex_buf_ptr] = right_brace) then
  7023.     begin
  7024.     decr_brace_level (pop_lit2);
  7025.     prev_colon := false;
  7026.     else
  7027.     if (brace_level = 0) then
  7028.         @<Convert a |brace_level = 0| character@>;
  7029.     incr(ex_buf_ptr);
  7030.     end;
  7031. check_brace_level (pop_lit2);
  7032. @^special character@>
  7033. We're dealing with a special character (usually either an undotted
  7034. `\i' or `\j', or an accent like one in Table~3.1 of the \LaTeX\
  7035. manual, or a foreign character like one in Table~3.2) if the first
  7036. character after the |left_brace| is a |backslash|; the special
  7037. character ends with the matching |right_brace|.  How we handle what's
  7038. in between depends on the special character.  In general, this code
  7039. will do reasonably well if there is other stuff, too, between braces,
  7040. but it doesn't try to do anything special with |colon|s.
  7041. @<Convert a special character@>=
  7042. begin
  7043. incr(ex_buf_ptr);            {skip over the |left_brace|}
  7044. while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
  7045.     begin
  7046.     incr(ex_buf_ptr);            {skip over the |backslash|}
  7047.     ex_buf_xptr := ex_buf_ptr;
  7048.     while ((ex_buf_ptr < ex_buf_length) and
  7049.         (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do
  7050.     incr(ex_buf_ptr);        {this scans the control sequence}
  7051.     control_seq_loc := str_lookup(ex_buf,ex_buf_xptr,ex_buf_ptr-ex_buf_xptr,
  7052.                         control_seq_ilk,dont_insert);
  7053.     if (hash_found) then
  7054.     @<Convert the accented or foreign character, if necessary@>;
  7055.     ex_buf_xptr := ex_buf_ptr;
  7056.     while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
  7057.                     (ex_buf[ex_buf_ptr] <> backslash)) do
  7058.     begin            {this scans to the next control sequence}
  7059.     if (ex_buf[ex_buf_ptr] = right_brace) then
  7060.         decr(brace_level)
  7061.     else if (ex_buf[ex_buf_ptr] = left_brace) then
  7062.         incr(brace_level);
  7063.     incr(ex_buf_ptr);
  7064.     end;
  7065.     @<Convert a noncontrol sequence@>;
  7066.     end;
  7067. decr(ex_buf_ptr);        {unskip the |right_brace|}
  7068. @^control sequence@>
  7069. @:this can't happen}{\quad Unknown type of case conversion@>
  7070. A control sequence, for the purposes of this program, consists just of
  7071. the consecutive alphabetic characters following the |backslash|; it
  7072. might be empty (although ones in this section aren't).
  7073. @<Convert the accented or foreign character, if necessary@>=
  7074. begin
  7075. case (conversion_type) of
  7076.     title_lowers,
  7077.     all_lowers :
  7078.     case (ilk_info[control_seq_loc]) of
  7079.         n_l_upper,
  7080.         n_o_upper,
  7081.         n_oe_upper,
  7082.         n_ae_upper,
  7083.         n_aa_upper :
  7084.         lower_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7085.         othercases
  7086.         do_nothing
  7087.     endcases;
  7088.     all_uppers :
  7089.     case (ilk_info[control_seq_loc]) of
  7090.         n_l,
  7091.         n_o,
  7092.         n_oe,
  7093.         n_ae,
  7094.         n_aa :
  7095.         upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7096.         n_i,
  7097.         n_j,
  7098.         n_ss :
  7099.         @<Convert, then remove the control sequence@>;
  7100.         othercases
  7101.         do_nothing
  7102.     endcases;
  7103.     bad_conversion :
  7104.     do_nothing;
  7105.     othercases
  7106.     case_conversion_confusion
  7107. endcases;
  7108. @:this can't happen}{\quad Unknown type of case conversion@>
  7109. Another bug complaint.
  7110. @<Procedures and functions for all file I/O, error messages, and such@>=
  7111. procedure case_conversion_confusion;
  7112. begin
  7113. confusion ('Unknown type of case conversion');
  7114. After converting the control sequence, we need to remove the preceding
  7115. |backslash| and any following |white_space|.
  7116. @<Convert, then remove the control sequence@>=
  7117. begin
  7118. upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7119. while (ex_buf_xptr < ex_buf_ptr) do
  7120.     begin            {remove preceding |backslash| and shift down}
  7121.     ex_buf[ex_buf_xptr-1] := ex_buf[ex_buf_xptr];
  7122.     incr(ex_buf_xptr);
  7123.     end;
  7124. decr(ex_buf_xptr);
  7125. while ((ex_buf_ptr < ex_buf_length) and
  7126.         (lex_class[ex_buf[ex_buf_ptr]] = white_space)) do
  7127.     incr(ex_buf_ptr);        {remove |white_space| trailing the control seq}
  7128. tmp_ptr := ex_buf_ptr;
  7129. while (tmp_ptr < ex_buf_length) do
  7130.     begin            {more shifting down}
  7131.     ex_buf[tmp_ptr-(ex_buf_ptr-ex_buf_xptr)] := ex_buf[tmp_ptr];
  7132.     incr(tmp_ptr)
  7133.     end;
  7134. ex_buf_length := tmp_ptr - (ex_buf_ptr - ex_buf_xptr);
  7135. ex_buf_ptr := ex_buf_xptr;
  7136. @:this can't happen}{\quad Unknown type of case conversion@>
  7137. There are no control sequences in what we're about to convert,
  7138. so a straight conversion suffices.
  7139. @<Convert a noncontrol sequence@>=
  7140. begin
  7141. case (conversion_type) of
  7142.     title_lowers,
  7143.     all_lowers :
  7144.     lower_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7145.     all_uppers :
  7146.     upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7147.     bad_conversion :
  7148.     do_nothing;
  7149.     othercases
  7150.     case_conversion_confusion
  7151. endcases;
  7152. @:this can't happen}{\quad Unknown type of case conversion@>
  7153. This code does any needed conversion for an ordinary character; it
  7154. won't touch nonletters.
  7155. @<Convert a |brace_level = 0| character@>=
  7156. begin
  7157. case (conversion_type) of
  7158.     title_lowers :
  7159.     begin
  7160.     if (ex_buf_ptr = 0) then
  7161.         do_nothing
  7162.     else if ((prev_colon) and
  7163.             (lex_class[ex_buf[ex_buf_ptr-1]] = white_space)) then
  7164.         do_nothing
  7165.     else
  7166.         lower_case (ex_buf, ex_buf_ptr, 1);
  7167.     if (ex_buf[ex_buf_ptr] = colon) then
  7168.         prev_colon := true
  7169.     else if (lex_class[ex_buf[ex_buf_ptr]] <> white_space) then
  7170.         prev_colon := false;
  7171.     end;
  7172.     all_lowers :
  7173.     lower_case (ex_buf, ex_buf_ptr, 1);
  7174.     all_uppers :
  7175.     upper_case (ex_buf, ex_buf_ptr, 1);
  7176.     bad_conversion :
  7177.     do_nothing;
  7178.     othercases
  7179.     case_conversion_confusion
  7180. endcases;
  7181. The |built_in| function {\.{chr.to.int\$}} pops the top (string)
  7182. literal, makes sure it's a single character, converts it to the
  7183. corresponding |ASCII_code| integer, and pushes this integer.  If the
  7184. literal isn't an appropriate string, it complains and pushes the
  7185. integer~0.
  7186. @<|execute_fn|({\.{chr.to.int\$}})@>=
  7187. procedure x_chr_to_int;
  7188. begin
  7189. pop_lit_stk (pop_lit1,pop_typ1);
  7190. if (pop_typ1 <> stk_str) then
  7191.     begin
  7192.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  7193.     push_lit_stk (0, stk_int);
  7194.     end
  7195. else if (length(pop_lit1) <> 1) then
  7196.     begin
  7197.     print ('"');
  7198.     print_pool_str (pop_lit1);
  7199.     bst_ex_warn ('" isn''t a single character');
  7200.     push_lit_stk (0, stk_int);
  7201.     end
  7202.     push_lit_stk (str_pool[str_start[pop_lit1]], stk_int);
  7203.                     {push the (|ASCII_code|) integer}
  7204. The |built_in| function {\.{cite\$}} pushes the appropriate string
  7205. from |cite_list| onto the stack.
  7206. @<|execute_fn|({\.{cite\$}})@>=
  7207. procedure x_cite;
  7208. begin
  7209. if (not mess_with_entries) then
  7210.     bst_cant_mess_with_entries_print
  7211.   else
  7212.     push_lit_stk (cur_cite_str, stk_str);
  7213. @^push the literal stack@>
  7214. The |built_in| function {\.{duplicate\$}} pops the top literal from
  7215. the stack and pushes two copies of it.
  7216. @<|execute_fn|({\.{duplicate\$}})@>=
  7217. procedure x_duplicate;
  7218. begin
  7219. pop_lit_stk (pop_lit1,pop_typ1);
  7220. if (pop_typ1 <> stk_str) then
  7221.     begin
  7222.     push_lit_stk (pop_lit1, pop_typ1);
  7223.     push_lit_stk (pop_lit1, pop_typ1);
  7224.     end
  7225.   else
  7226.     begin
  7227.     repush_string;
  7228.     if (pop_lit1 < cmd_str_ptr) then
  7229.     push_lit_stk (pop_lit1, pop_typ1)
  7230.       else
  7231.     begin
  7232.     str_room (length(pop_lit1));
  7233.     sp_ptr := str_start[pop_lit1];
  7234.     sp_end := str_start[pop_lit1+1];
  7235.     while (sp_ptr < sp_end) do
  7236.         begin
  7237.         append_char (str_pool[sp_ptr]);
  7238.         incr(sp_ptr);
  7239.         end;
  7240.     push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  7241.     end;
  7242.     end;
  7243. The |built_in| function {\.{empty\$}} pops the top literal and pushes
  7244. the integer 1 if it's a missing field or a string having no
  7245. non|white_space| characters, 0 otherwise.  If the literal isn't a
  7246. missing field or a string, it complains and pushes 0.
  7247. @<|execute_fn|({\.{empty\$}})@>=
  7248. procedure x_empty;
  7249. label exit;
  7250. begin
  7251. pop_lit_stk (pop_lit1,pop_typ1);
  7252. case (pop_typ1) of
  7253.     stk_str : @<Push 0 if the string has a non|white_space| char, else 1@>;
  7254.     stk_field_missing : push_lit_stk (1, stk_int);
  7255.     stk_empty : push_lit_stk (0, stk_int);
  7256.     othercases
  7257.     begin
  7258.     print_stk_lit (pop_lit1,pop_typ1);
  7259.     bst_ex_warn (', not a string or missing field,');
  7260.     push_lit_stk (0, stk_int);
  7261. endcases;
  7262. exit:
  7263. When we arrive here we're dealing with a legitimate string.  If it has
  7264. no characters, or has nothing but |white_space| characters, we push~1,
  7265. otherwise we push~0.
  7266. @<Push 0 if the string has a non|white_space| char, else 1@>=
  7267. begin
  7268. sp_ptr := str_start[pop_lit1];
  7269. sp_end := str_start[pop_lit1+1];
  7270. while (sp_ptr < sp_end) do
  7271.     begin
  7272.     if (lex_class[str_pool[sp_ptr]] <> white_space) then
  7273.     begin
  7274.     push_lit_stk (0, stk_int);
  7275.     return;
  7276.     end;
  7277.     incr(sp_ptr);
  7278.     end;
  7279. push_lit_stk (1, stk_int);
  7280. The |built_in| function {\.{format.name\$}} pops the top three
  7281. literals (they are a string, an integer, and a string literal, in that
  7282. order).  The last string literal represents a name list (each name
  7283. corresponding to a person), the integer literal specifies which name
  7284. to pick from this list, and the first string literal specifies how to
  7285. format this name, as described in the \BibTeX\ documentation.
  7286. Finally, this function pushes the formatted name.  If any of the types
  7287. is incorrect, it complains and pushes the null string.
  7288. @d von_found = 52        {for when a von token is found}
  7289. @<|execute_fn|({\.{format.name\$}})@>=
  7290. procedure x_format_name;
  7291. label loop1_exit,@!loop2_exit,@!von_found;
  7292. begin
  7293. pop_lit_stk (pop_lit1,pop_typ1);
  7294. pop_lit_stk (pop_lit2,pop_typ2);
  7295. pop_lit_stk (pop_lit3,pop_typ3);
  7296. if (pop_typ1 <> stk_str) then
  7297.     begin
  7298.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  7299.     push_lit_stk (s_null, stk_str);
  7300.     end
  7301. else if (pop_typ2 <> stk_int) then
  7302.     begin
  7303.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  7304.     push_lit_stk (s_null, stk_str);
  7305.     end
  7306. else if (pop_typ3 <> stk_str) then
  7307.     begin
  7308.     print_wrong_stk_lit (pop_lit3,pop_typ3,stk_str);
  7309.     push_lit_stk (s_null, stk_str);
  7310.     end
  7311.     begin
  7312.     ex_buf_length := 0;
  7313.     add_buf_pool (pop_lit3);
  7314.     @<Isolate the desired name@>;
  7315.     @<Copy name and count |comma|s to determine syntax@>;
  7316.     @<Find the parts of the name@>;
  7317.     ex_buf_length := 0;
  7318.     add_buf_pool (pop_lit1);
  7319.     figure_out_the_formatted_name;@/
  7320.     add_pool_buf_and_push;    {push the formatted string onto the stack}
  7321.     end;
  7322. This module skips over undesired names in |pop_lit3| and it throws
  7323. away the ``and'' from the end of the name if it exists.  When it's
  7324. done, |ex_buf_xptr| points to its first character and |ex_buf_ptr|
  7325. points just past its last.
  7326. @<Isolate the desired name@>=
  7327. begin
  7328. ex_buf_ptr := 0;
  7329. num_names := 0;
  7330. while ((num_names < pop_lit2) and (ex_buf_ptr < ex_buf_length)) do
  7331.     begin
  7332.     incr(num_names);
  7333.     ex_buf_xptr := ex_buf_ptr;
  7334.     name_scan_for_and (pop_lit3);
  7335.     end;
  7336. if (ex_buf_ptr < ex_buf_length) then        {remove the ``and''}
  7337.     ex_buf_ptr := ex_buf_ptr - 4;
  7338. if (num_names < pop_lit2) then
  7339.     begin
  7340.     if (pop_lit2 = 1) then
  7341.     print ('There is no name in "')
  7342.       else
  7343.     print ('There aren''t ',pop_lit2:0,' names in "');
  7344.     print_pool_str (pop_lit3);
  7345.     bst_ex_warn ('"');
  7346.     end
  7347. This module, starting at |ex_buf_ptr|, looks in |ex_buf| for an
  7348. ``and'' surrounded by nonnull |white_space|.  It stops either at
  7349. |ex_buf_length| or just past the ``and'', whichever comes first,
  7350. setting |ex_buf_ptr| accordingly.  Its parameter |pop_lit_var| is
  7351. either |pop_lit3| or |pop_lit1|, depending on whether
  7352. {\.{format.name\$}} or {\.{num.names\$}} calls it.
  7353. @<Procedures and functions for name-string processing@>=
  7354. procedure name_scan_for_and (@!pop_lit_var : str_number);
  7355. begin
  7356. brace_level := 0;
  7357. preceding_white := false;
  7358. and_found := false;
  7359. while ((not and_found) and (ex_buf_ptr < ex_buf_length)) do
  7360.   case (ex_buf[ex_buf_ptr]) of
  7361.     "a", "A" :
  7362.     begin
  7363.     incr(ex_buf_ptr);
  7364.     if (preceding_white) then
  7365.         @<See if we have an ``and''@>;    {if so, |and_found := true|}
  7366.     preceding_white := false;
  7367.     end;
  7368.     left_brace :
  7369.     begin
  7370.     incr(brace_level);
  7371.     incr(ex_buf_ptr);
  7372.     @<Skip over |ex_buf| stuff at |brace_level > 0|@>;
  7373.     preceding_white := false;
  7374.     end;
  7375.     right_brace :
  7376.     begin
  7377.     decr_brace_level (pop_lit_var);        {this checks for an error}
  7378.     incr(ex_buf_ptr);
  7379.     preceding_white := false;
  7380.     end;
  7381.     othercases
  7382.     if (lex_class[ex_buf[ex_buf_ptr]] = white_space) then
  7383.         begin
  7384.         incr(ex_buf_ptr);
  7385.         preceding_white := true;
  7386.         end
  7387.     else
  7388.         begin
  7389.         incr(ex_buf_ptr);
  7390.         preceding_white := false;
  7391.         end
  7392.   endcases;
  7393. check_brace_level (pop_lit_var);
  7394. When we come here |ex_buf_ptr| is just past the |left_brace|, and when
  7395. we leave it's either at |ex_buf_length| or just past the matching
  7396. |right_brace|.
  7397. @<Skip over |ex_buf| stuff at |brace_level > 0|@>=
  7398. while ((brace_level > 0) and (ex_buf_ptr < ex_buf_length)) do
  7399.     begin
  7400.     if (ex_buf[ex_buf_ptr] = right_brace) then
  7401.     decr(brace_level)
  7402.     else if (ex_buf[ex_buf_ptr] = left_brace) then
  7403.     incr(brace_level);
  7404.     incr(ex_buf_ptr);
  7405.     end
  7406. When we come here |ex_buf_ptr| is just past the ``a'' or ``A'', and when
  7407. we leave it's either at the same place or, if we found an ``and'', at
  7408. the following |white_space| character.
  7409. @<See if we have an ``and''@>=
  7410. begin
  7411. if (ex_buf_ptr <= (ex_buf_length - 3)) then    {enough characters are left}
  7412.     if ((ex_buf[ex_buf_ptr] = "n") or (ex_buf[ex_buf_ptr] = "N")) then
  7413.     if ((ex_buf[ex_buf_ptr+1] = "d") or (ex_buf[ex_buf_ptr+1] = "D")) then
  7414.         if (lex_class[ex_buf[ex_buf_ptr+2]] = white_space) then
  7415.         begin
  7416.         ex_buf_ptr := ex_buf_ptr + 2;
  7417.         and_found := true;
  7418.         end;
  7419. When we arrive here, the desired name is in |ex_buf[ex_buf_xptr]|
  7420. through |ex_buf[ex_buf_ptr-1]|.  This module does its thing for
  7421. characters only at |brace_level = 0|; the rest get processed verbatim.
  7422. It removes leading |white_space| (and |sep_char|s), and trailing
  7423. |white_space| (and |sep_char|s) and |comma|s, complaining for each
  7424. trailing |comma|.  It then copies the name into |name_buf|, removing
  7425. all |white_space|, |sep_char|s and |comma|s, counting |comma|s, and
  7426. constructing a list of name tokens, which are sequences of characters
  7427. separated (at |brace_level=0|) by |white_space|, |sep_char|s or
  7428. |comma|s.  Each name token but the first has an associated
  7429. |name_sep_char|, the character that separates it from the preceding
  7430. token.  If there are too many (more than two) |comma|s, a complaint is
  7431. in order.
  7432. @<Copy name and count |comma|s to determine syntax@>=
  7433. begin
  7434. @<Remove leading and trailing junk, complaining if necessary@>;
  7435. name_bf_ptr := 0;
  7436. num_commas := 0;
  7437. num_tokens := 0;@/
  7438. token_starting := true;        {to indicate that a name token is starting}
  7439. while (ex_buf_xptr < ex_buf_ptr) do
  7440.     case (ex_buf[ex_buf_xptr]) of
  7441.     comma : @<Name-process a |comma|@>;
  7442.     left_brace : @<Name-process a |left_brace|@>;
  7443.     right_brace : @<Name-process a |right_brace|@>;
  7444.     othercases
  7445.         case (lex_class[ex_buf[ex_buf_xptr]]) of
  7446.         white_space : @<Name-process a |white_space|@>;
  7447.         sep_char : @<Name-process a |sep_char|@>;
  7448.         othercases @<Name-process some other character@>
  7449.         endcases
  7450.     endcases;
  7451. name_tok[num_tokens] := name_bf_ptr;    {this is an end-marker}
  7452. This module removes all leading |white_space| (and |sep_char|s), and
  7453. trailing |white_space| (and |sep_char|s) and |comma|s.  It complains
  7454. for each trailing |comma|.
  7455. @<Remove leading and trailing junk, complaining if necessary@>=
  7456. begin
  7457. while ((ex_buf_xptr < ex_buf_ptr) and
  7458.             (lex_class[ex_buf[ex_buf_ptr]] = white_space) and
  7459.             (lex_class[ex_buf[ex_buf_ptr]] = sep_char)) do
  7460.     incr(ex_buf_xptr);            {this removes leading stuff}
  7461. while (ex_buf_ptr > ex_buf_xptr) do        {now remove trailing stuff}
  7462.     case (lex_class[ex_buf[ex_buf_ptr-1]]) of
  7463.     white_space,
  7464.     sep_char :
  7465.         decr(ex_buf_ptr);
  7466.     othercases
  7467.         if (ex_buf[ex_buf_ptr-1] = comma) then
  7468.         begin
  7469.         print ('Name ',pop_lit2:0,' in "');
  7470.         print_pool_str (pop_lit3);
  7471.         print ('" has a comma at the end');
  7472.         bst_ex_warn_print;
  7473.         decr(ex_buf_ptr);
  7474.         end
  7475.         else
  7476.         goto loop1_exit
  7477.     endcases;
  7478. loop1_exit:
  7479. Here we mark the token number at which this comma has occurred.
  7480. @<Name-process a |comma|@>=
  7481. begin
  7482. if (num_commas = 2) then
  7483.     begin
  7484.     print ('Too many commas in name ',pop_lit2:0,' of "');
  7485.     print_pool_str (pop_lit3);
  7486.     print ('"');
  7487.     bst_ex_warn_print;
  7488.     end
  7489.   else
  7490.     begin
  7491.     incr(num_commas);
  7492.     if (num_commas = 1) then
  7493.     comma1 := num_tokens
  7494.       else
  7495.     comma2 := num_tokens;            {|num_commas = 2|}
  7496.     name_sep_char[num_tokens] := comma;
  7497.     end;
  7498. incr(ex_buf_xptr);
  7499. token_starting := true;
  7500. We copy the stuff up through the matching |right_brace| verbatim.
  7501. @<Name-process a |left_brace|@>=
  7502. begin
  7503. incr(brace_level);
  7504. if (token_starting) then
  7505.     begin
  7506.     name_tok[num_tokens] := name_bf_ptr;
  7507.     incr(num_tokens);
  7508.     end;
  7509. name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
  7510. incr(name_bf_ptr);
  7511. incr(ex_buf_xptr);
  7512. while ((brace_level > 0) and (ex_buf_xptr < ex_buf_ptr)) do
  7513.     begin
  7514.     if (ex_buf[ex_buf_xptr] = right_brace) then
  7515.     decr(brace_level)
  7516.     else if (ex_buf[ex_buf_xptr] = left_brace) then
  7517.     incr(brace_level);
  7518.     name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
  7519.     incr(name_bf_ptr);
  7520.     incr(ex_buf_xptr);
  7521.     end;
  7522. token_starting := false;
  7523. We don't copy an extra |right_brace|; this code will almost never be
  7524. executed.
  7525. @<Name-process a |right_brace|@>=
  7526. begin
  7527. if (token_starting) then
  7528.     begin
  7529.     name_tok[num_tokens] := name_bf_ptr;
  7530.     incr(num_tokens);
  7531.     end;
  7532. print ('Name ',pop_lit2:0,' of "');
  7533. print_pool_str (pop_lit3);
  7534. bst_ex_warn ('" isn''t brace balanced');
  7535. incr(ex_buf_xptr);
  7536. token_starting := false;
  7537. A token will be starting soon in a buffer near you, one way$\ldots$
  7538. @<Name-process a |white_space|@>=
  7539. begin
  7540. if (not token_starting) then
  7541.     name_sep_char[num_tokens] := space;
  7542. incr(ex_buf_xptr);
  7543. token_starting := true;
  7544. @^user abuse@>
  7545. or another.  If one of the valid |sep_char|s appears between tokens,
  7546. we usually use it instead of a |space|.  If the user has been silly
  7547. enough to have multiple |sep_char|s, or to have both |white_space| and
  7548. a |sep_char|, we use the first such character.
  7549. @<Name-process a |sep_char|@>=
  7550. begin
  7551. if (not token_starting) then
  7552.     name_sep_char[num_tokens] := ex_buf[ex_buf_xptr];
  7553. incr(ex_buf_xptr);
  7554. token_starting := true;
  7555. For ordinary characters, we just copy the character.
  7556. @<Name-process some other character@>=
  7557. begin
  7558. if (token_starting) then
  7559.     begin
  7560.     name_tok[num_tokens] := name_bf_ptr;
  7561.     incr(num_tokens);
  7562.     end;
  7563. name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
  7564. incr(name_bf_ptr);
  7565. incr(ex_buf_xptr);
  7566. token_starting := false;
  7567. @:this can't happen}{\quad Illegal number of comma,s@>
  7568. Here we set all the pointers for the various parts of the name,
  7569. depending on which of the three possible syntaxes this name uses.
  7570. @<Find the parts of the name@>=
  7571. begin
  7572. if (num_commas = 0) then
  7573.     begin
  7574.     first_start := 0;
  7575.     last_end := num_tokens;
  7576.     jr_end := last_end;
  7577.     @<Determine where the first name ends and von name starts and ends@>;
  7578.     end
  7579. else if (num_commas = 1) then
  7580.     begin
  7581.     von_start := 0;
  7582.     last_end := comma1;
  7583.     jr_end := last_end;
  7584.     first_start := jr_end;
  7585.     first_end := num_tokens;
  7586.     von_name_ends_and_last_name_starts_stuff;
  7587.     end
  7588. else if (num_commas = 2) then
  7589.     begin
  7590.     von_start := 0;
  7591.     last_end := comma1;
  7592.     jr_end := comma2;
  7593.     first_start := jr_end;
  7594.     first_end := num_tokens;
  7595.     von_name_ends_and_last_name_starts_stuff;
  7596.     end
  7597.     confusion ('Illegal number of comma,s');
  7598. When there are no brace-level-0 |comma|s in the name, the von name
  7599. starts with the first nonlast token whose first brace-level-0 letter
  7600. is in lower case (for the purposes of this determination, an accented
  7601. or foreign character at brace-level-1 that's in lower case will do, as
  7602. well).  A module following this one determines where the von name ends
  7603. and the last starts.
  7604. @<Determine where the first name ends and von name starts and ends@>=
  7605. begin
  7606. von_start := 0;
  7607. while (von_start < last_end-1) do
  7608.     begin
  7609.     name_bf_ptr := name_tok[von_start];
  7610.     name_bf_xptr := name_tok[von_start+1];
  7611.     if (von_token_found) then
  7612.     begin
  7613.     von_name_ends_and_last_name_starts_stuff;
  7614.     goto von_found;
  7615.     end;
  7616.     incr(von_start);
  7617.     end;            {there's no von name, so}
  7618. while (von_start > 0) do    {backtrack if there are connected tokens}
  7619.     begin
  7620.     if ((lex_class[name_sep_char[von_start]] <> sep_char) or
  7621.             (name_sep_char[von_start] = tie)) then
  7622.     goto loop2_exit;
  7623.     decr(von_start);
  7624.     end;
  7625. loop2_exit:
  7626. von_end := von_start;
  7627. von_found:
  7628. first_end := von_start;
  7629. @^special character@>
  7630. It's a von token if there exists a first brace-level-0 letter (or
  7631. brace-level-1 special character), and it's in lower case; in this case
  7632. we return |true|.  The token is in |name_buf|, starting at
  7633. |name_bf_ptr| and ending just before |name_bf_xptr|.
  7634. @d return_von_found ==    begin
  7635.             von_token_found := true;
  7636.             return;
  7637.             end
  7638. @<Procedures and functions for name-string processing@>=
  7639. function von_token_found : boolean;
  7640. label exit;
  7641. begin
  7642. nm_brace_level := 0;
  7643. von_token_found := false;        {now it's easy to exit if necessary}
  7644. while (name_bf_ptr < name_bf_xptr) do
  7645.     if ((name_buf[name_bf_ptr] >= "A") and
  7646.             (name_buf[name_bf_ptr] <= "Z")) then
  7647.     return
  7648.     else if ((name_buf[name_bf_ptr] >= "a") and
  7649.             (name_buf[name_bf_ptr] <= "z")) then
  7650.     return_von_found
  7651.     else if (name_buf[name_bf_ptr] = left_brace) then
  7652.     begin
  7653.     incr(nm_brace_level);
  7654.     incr(name_bf_ptr);
  7655.     if ((name_bf_ptr + 2 < name_bf_xptr) and
  7656.                 (name_buf[name_bf_ptr] = backslash)) then
  7657.         @<Check the special character (and |return|)@>
  7658.       else
  7659.         @<Skip over |name_buf| stuff at |nm_brace_level > 0|@>;
  7660.     else
  7661.     incr(name_bf_ptr);
  7662. exit:
  7663. @^special character@>
  7664. When we come here |name_bf_ptr| is just past the |left_brace|,
  7665. but we always leave by |return|ing.
  7666. @<Check the special character (and |return|)@>=
  7667. begin
  7668. incr(name_bf_ptr);            {skip over the |backslash|}
  7669. name_bf_yptr := name_bf_ptr;
  7670. while ((name_bf_ptr < name_bf_xptr) and
  7671.         (lex_class[name_buf[name_bf_ptr]] = alpha)) do
  7672.     incr(name_bf_ptr);            {this scans the control sequence}
  7673. control_seq_loc := str_lookup(name_buf,name_bf_yptr,name_bf_ptr-name_bf_yptr,
  7674.                         control_seq_ilk,dont_insert);
  7675. if (hash_found) then
  7676.     @<Handle this accented or foreign character (and |return|)@>;
  7677. while ((name_bf_ptr < name_bf_xptr) and (nm_brace_level > 0)) do
  7678.     begin
  7679.     if ((name_buf[name_bf_ptr] >= "A") and
  7680.             (name_buf[name_bf_ptr] <= "Z")) then
  7681.     return
  7682.     else if ((name_buf[name_bf_ptr] >= "a") and
  7683.             (name_buf[name_bf_ptr] <= "z")) then
  7684.     return_von_found
  7685.     else if (name_buf[name_bf_ptr] = right_brace) then
  7686.     decr(nm_brace_level)
  7687.     else if (name_buf[name_bf_ptr] = left_brace) then
  7688.     incr(nm_brace_level);
  7689.     incr(name_bf_ptr);
  7690.     end;
  7691. return;
  7692. @:this can't happen}{\quad Control-sequence hash error@>
  7693. The accented or foreign character is either `\.{\\i}' or `\.{\\j}' or
  7694. one of the eleven alphabetic foreign characters in Table~3.2 of the
  7695. \LaTeX\ manual.
  7696. @<Handle this accented or foreign character (and |return|)@>=
  7697. begin
  7698. case (ilk_info[control_seq_loc]) of
  7699.     n_oe_upper,
  7700.     n_ae_upper,
  7701.     n_aa_upper,
  7702.     n_o_upper,
  7703.     n_l_upper :
  7704.     return;
  7705.     n_i,
  7706.     n_j,
  7707.     n_oe,
  7708.     n_ae,
  7709.     n_aa,
  7710.     n_o,
  7711.     n_l,
  7712.     n_ss :
  7713.     return_von_found;
  7714.     othercases
  7715.     confusion ('Control-sequence hash error')
  7716. endcases;
  7717. When we come here |name_bf_ptr| is just past the |left_brace|; when we
  7718. leave it's either at |name_bf_xptr| or just past the matching
  7719. |right_brace|.
  7720. @<Skip over |name_buf| stuff at |nm_brace_level > 0|@>=
  7721. while ((nm_brace_level > 0) and (name_bf_ptr < name_bf_xptr)) do
  7722.     begin
  7723.     if (name_buf[name_bf_ptr] = right_brace) then
  7724.     decr(nm_brace_level)
  7725.     else if (name_buf[name_bf_ptr] = left_brace) then
  7726.     incr(nm_brace_level);
  7727.     incr(name_bf_ptr);
  7728.     end
  7729. @^Casey Stengel would be proud@>
  7730. @^special character@>
  7731. @^Tuesdays@>
  7732. The last name starts just past the last token, before the first
  7733. |comma| (if there is no |comma|, there is deemed to be one at the end
  7734. of the string), for which there exists a first brace-level-0 letter
  7735. (or brace-level-1 special character), and it's in lower case, unless
  7736. this last token is also the last token before the |comma|, in which
  7737. case the last name starts with this token (unless this last token is
  7738. connected by a |sep_char| other than a |tie| to the previous token, in
  7739. which case the last name starts with as many tokens earlier as are
  7740. connected by non|tie|s to this last one (except on Tuesdays
  7741. $\ldots\,$), although this module never sees such a case).  Note that
  7742. if there are any tokens in either the von or last names, then the last
  7743. name has at least one, even if it starts with a lower-case letter.
  7744. @<Procedures and functions for name-string processing@>=
  7745. procedure von_name_ends_and_last_name_starts_stuff;
  7746. label exit;
  7747. begin                {there may or may not be a von name}
  7748. von_end := last_end - 1;
  7749. while (von_end > von_start) do
  7750.     begin
  7751.     name_bf_ptr := name_tok[von_end-1];
  7752.     name_bf_xptr := name_tok[von_end];
  7753.     if (von_token_found) then
  7754.     return;
  7755.     decr(von_end);
  7756.     end;
  7757. exit:
  7758. This module uses the information in |pop_lit1| to format the name.
  7759. Everything at |sp_brace_level = 0| is copied verbatim to the formatted
  7760. string; the rest is described in the succeeding modules.
  7761. @<Figure out the formatted name@>=
  7762. begin
  7763. ex_buf_ptr := 0;
  7764. sp_brace_level := 0;
  7765. sp_ptr := str_start[pop_lit1];
  7766. sp_end := str_start[pop_lit1+1];
  7767. while (sp_ptr < sp_end) do
  7768.     if (str_pool[sp_ptr] = left_brace) then
  7769.     begin
  7770.     incr(sp_brace_level);
  7771.     incr(sp_ptr);
  7772.     @<Format this part of the name@>;
  7773.     else if (str_pool[sp_ptr] = right_brace) then
  7774.     begin
  7775.     braces_unbalanced_complaint (pop_lit1);
  7776.     incr(sp_ptr);
  7777.     else
  7778.     begin
  7779.     append_ex_buf_char_and_check (str_pool[sp_ptr]);
  7780.     incr(sp_ptr);
  7781.     end;
  7782. if (sp_brace_level > 0) then
  7783.     braces_unbalanced_complaint (pop_lit1);
  7784. ex_buf_length := ex_buf_ptr;
  7785. When we arrive here we're at |sp_brace_level = 1|, just past the
  7786. |left_brace|.  Letters at this |sp_brace_level| other than those
  7787. denoting the parts of the name (i.e., the first letters of `first,'
  7788. `last,' `von,' and `jr,' ignoring case) are illegal.  We do two passes
  7789. over this group; the first determines whether we're to output
  7790. anything, and, if we are, the second actually outputs it.
  7791. @<Format this part of the name@>=
  7792. begin
  7793. sp_xptr1 := sp_ptr;
  7794. alpha_found := false;
  7795. double_letter := false;
  7796. end_of_group := false;
  7797. to_be_written := true;
  7798. while ((not end_of_group) and (sp_ptr < sp_end)) do
  7799.     if (lex_class[str_pool[sp_ptr]] = alpha) then
  7800.     begin
  7801.     incr(sp_ptr);
  7802.     @<Figure out what this letter means@>;
  7803.     else if (str_pool[sp_ptr] = right_brace) then
  7804.     begin
  7805.     decr(sp_brace_level);
  7806.     incr(sp_ptr);
  7807.     end_of_group := true;
  7808.     else if (str_pool[sp_ptr] = left_brace) then
  7809.     begin
  7810.     incr(sp_brace_level);
  7811.     incr(sp_ptr);
  7812.     skip_stuff_at_sp_brace_level_greater_than_one;
  7813.     else
  7814.     incr(sp_ptr);
  7815. if ((end_of_group) and (to_be_written)) then    {do the second pass}
  7816.     @<Finally format this part of the name@>;
  7817. When we come here |sp_ptr| is just past the |left_brace|, and when we
  7818. leave it's either at |sp_end| or just past the matching |right_brace|.
  7819. @<Procedures and functions for name-string processing@>=
  7820. procedure skip_stuff_at_sp_brace_level_greater_than_one;
  7821. begin
  7822. while ((sp_brace_level > 1) and (sp_ptr < sp_end)) do
  7823.     begin
  7824.     if (str_pool[sp_ptr] = right_brace) then
  7825.     decr(sp_brace_level)
  7826.     else if (str_pool[sp_ptr] = left_brace) then
  7827.     incr(sp_brace_level);
  7828.     incr(sp_ptr);
  7829.     end;
  7830. We won't output anything for this part of the name if this is a second
  7831. occurrence of an |sp_brace_level = 1| letter, if it's an illegal
  7832. letter, or if there are no tokens corresponding to this part.  We also
  7833. determine if we're we to output complete tokens (indicated by a double
  7834. letter).
  7835. @<Figure out what this letter means@>=
  7836. begin
  7837. if (alpha_found) then
  7838.     begin
  7839.     brace_lvl_one_letters_complaint;
  7840.     to_be_written := false;
  7841.     end
  7842.   else
  7843.     begin
  7844.     case (str_pool[sp_ptr-1]) of
  7845.     "f","F" : @<Figure out what tokens we'll output for the `first' name@>;
  7846.     "v","V" : @<Figure out what tokens we'll output for the `von' name@>;
  7847.     "l","L" : @<Figure out what tokens we'll output for the `last' name@>;
  7848.     "j","J" : @<Figure out what tokens we'll output for the `jr' name@>;
  7849.     othercases
  7850.         begin
  7851.         brace_lvl_one_letters_complaint;
  7852.         to_be_written := false;
  7853.         end
  7854.     endcases;
  7855.     if (double_letter) then
  7856.     incr(sp_ptr);
  7857.     end;
  7858. alpha_found := true;
  7859. At most one of the important letters, perhaps doubled, may appear at
  7860. |sp_brace_level = 1|.
  7861. @<Procedures and functions for name-string processing@>=
  7862. procedure brace_lvl_one_letters_complaint;
  7863. begin
  7864. print ('The format string "');
  7865. print_pool_str (pop_lit1);
  7866. bst_ex_warn ('" has an illegal brace-level-1 letter');
  7867. Here we set pointers into |name_tok| and note whether we'll be dealing
  7868. with a full first-name tokens (|double_letter = true|) or
  7869. abbreviations (|double_letter = false|).
  7870. @<Figure out what tokens we'll output for the `first' name@>=
  7871. begin
  7872. cur_token := first_start;
  7873. last_token := first_end;
  7874. if (cur_token = last_token) then
  7875.     to_be_written := false;
  7876. if ((str_pool[sp_ptr] = "f") or (str_pool[sp_ptr] = "F")) then
  7877.     double_letter := true;
  7878. The same as above but for von-name tokens.
  7879. @<Figure out what tokens we'll output for the `von' name@>=
  7880. begin
  7881. cur_token := von_start;
  7882. last_token := von_end;
  7883. if (cur_token = last_token) then
  7884.     to_be_written := false;
  7885. if ((str_pool[sp_ptr] = "v") or (str_pool[sp_ptr] = "V")) then
  7886.     double_letter := true;
  7887. The same as above but for last-name tokens.
  7888. @<Figure out what tokens we'll output for the `last' name@>=
  7889. begin
  7890. cur_token := von_end;
  7891. last_token := last_end;
  7892. if (cur_token = last_token) then
  7893.     to_be_written := false;
  7894. if ((str_pool[sp_ptr] = "l") or (str_pool[sp_ptr] = "L")) then
  7895.     double_letter := true;
  7896. The same as above but for jr-name tokens.
  7897. @<Figure out what tokens we'll output for the `jr' name@>=
  7898. begin
  7899. cur_token := last_end;
  7900. last_token := jr_end;
  7901. if (cur_token = last_token) then
  7902.     to_be_written := false;
  7903. if ((str_pool[sp_ptr] = "j") or (str_pool[sp_ptr] = "J")) then
  7904.     double_letter := true;
  7905. This is the second pass over this part of the name; here we actually
  7906. write stuff out to |ex_buf|.
  7907. @<Finally format this part of the name@>=
  7908. begin
  7909. ex_buf_xptr := ex_buf_ptr;
  7910. sp_ptr := sp_xptr1;
  7911. sp_brace_level := 1;
  7912. while (sp_brace_level > 0) do
  7913.     if ((lex_class[str_pool[sp_ptr]] = alpha) and (sp_brace_level = 1)) then
  7914.     begin
  7915.     incr(sp_ptr);
  7916.     @<Figure out how to output the name tokens, and do it@>;
  7917.     else if (str_pool[sp_ptr] = right_brace) then
  7918.     begin
  7919.     decr(sp_brace_level);
  7920.     incr(sp_ptr);
  7921.     if (sp_brace_level > 0) then
  7922.         append_ex_buf_char_and_check (right_brace);
  7923.     else if (str_pool[sp_ptr] = left_brace) then
  7924.     begin
  7925.     incr(sp_brace_level);
  7926.     incr(sp_ptr);
  7927.     append_ex_buf_char_and_check (left_brace);
  7928.     else
  7929.     begin
  7930.     append_ex_buf_char_and_check (str_pool[sp_ptr]);
  7931.     incr(sp_ptr);
  7932.     end;
  7933. if (ex_buf_ptr > 0) then
  7934.   if (ex_buf[ex_buf_ptr-1] = tie) then
  7935.     @<Handle a discretionary |tie|@>;
  7936. When we come here, |sp_ptr| is just past the letter indicating the
  7937. part of the name for which we're about to output tokens.  When we
  7938. leave, it's at the first character of the rest of the group.
  7939. @<Figure out how to output the name tokens, and do it@>=
  7940. begin
  7941. if (double_letter) then
  7942.     incr(sp_ptr);
  7943. use_default := true;
  7944. sp_xptr2 := sp_ptr;
  7945. if (str_pool[sp_ptr] = left_brace) then        {find the inter-token string}
  7946.     begin
  7947.     use_default := false;
  7948.     incr(sp_brace_level);
  7949.     incr(sp_ptr);
  7950.     sp_xptr1 := sp_ptr;
  7951.     skip_stuff_at_sp_brace_level_greater_than_one;
  7952.     sp_xptr2 := sp_ptr - 1;
  7953.     end;
  7954. @<Finally output the name tokens@>;
  7955. if (not use_default) then
  7956.     sp_ptr := sp_xptr2 + 1;
  7957. Here, for each token in this part, we output either a full or an
  7958. abbreviated token and the inter-token string for all but the last
  7959. token of this part.
  7960. @<Finally output the name tokens@>=
  7961. while (cur_token < last_token) do
  7962.     begin
  7963.     if (double_letter) then
  7964.     @<Finally output a full token@>
  7965.       else
  7966.     @<Finally output an abbreviated token@>;
  7967.     incr(cur_token);
  7968.     if (cur_token < last_token) then
  7969.     @<Finally output the inter-token string@>;
  7970.     end
  7971. @:BibTeX capacity exceeded}{\quad buffer size@>
  7972. Here we output all the characters in the token, verbatim.
  7973. @<Finally output a full token@>=
  7974. begin
  7975. name_bf_ptr := name_tok[cur_token];
  7976. name_bf_xptr := name_tok[cur_token+1];
  7977. if (ex_buf_length+(name_bf_xptr-name_bf_ptr) > buf_size) then
  7978.     buffer_overflow;
  7979. while (name_bf_ptr < name_bf_xptr) do
  7980.     begin
  7981.     append_ex_buf_char (name_buf[name_bf_ptr]);
  7982.     incr(name_bf_ptr);
  7983.     end;
  7984. @^special character@>
  7985. Here we output the first alphabetic or special character of the token;
  7986. brace level is irrelevant for an alphabetic (but not a special)
  7987. character.
  7988. @<Finally output an abbreviated token@>=
  7989. begin
  7990. name_bf_ptr := name_tok[cur_token];
  7991. name_bf_xptr := name_tok[cur_token+1];
  7992. while (name_bf_ptr < name_bf_xptr) do
  7993.     begin
  7994.     if (lex_class[name_buf[name_bf_ptr]] = alpha) then
  7995.     begin
  7996.     append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
  7997.     goto loop_exit;
  7998.     else if ((name_buf[name_bf_ptr] = left_brace) and
  7999.                 (name_bf_ptr + 1 < name_bf_xptr)) then
  8000.       if (name_buf[name_bf_ptr+1] = backslash) then
  8001.     @<Finally output a special character and exit loop@>;
  8002.     incr(name_bf_ptr);
  8003.     end;
  8004. loop_exit:
  8005. @^special character@>
  8006. @^user abuse@>
  8007. @:BibTeX capacity exceeded}{\quad buffer size@>
  8008. We output a special character here even if the user has been silly
  8009. enough to make it nonalphabetic (and even if the user has been sillier
  8010. still by not having a matching |right_brace|).
  8011. @<Finally output a special character and exit loop@>=
  8012. begin
  8013. if (ex_buf_ptr + 2 > buf_size) then
  8014.     buffer_overflow;
  8015. append_ex_buf_char (left_brace);
  8016. append_ex_buf_char (backslash);
  8017. name_bf_ptr := name_bf_ptr + 2;
  8018. nm_brace_level := 1;
  8019. while ((name_bf_ptr < name_bf_xptr) and (nm_brace_level > 0)) do
  8020.     begin
  8021.     if (name_buf[name_bf_ptr] = right_brace) then
  8022.     decr(nm_brace_level)
  8023.     else if (name_buf[name_bf_ptr] = left_brace) then
  8024.     incr(nm_brace_level);
  8025.     append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
  8026.     incr(name_bf_ptr);
  8027.     end;
  8028. goto loop_exit;
  8029. @:BibTeX capacity exceeded}{\quad buffer size@>
  8030. Here we output either the \.{.bst} given string if it exists, or else
  8031. the \.{.bib} |sep_char| if it exists, or else the default string.  A
  8032. |tie| is the default space character between the last two tokens of
  8033. the name part, and between the first two tokens if the first token is
  8034. short enough; otherwise, a |space| is the default.
  8035. @d long_token = 3    {a token this length or longer is ``long''}
  8036. @<Finally output the inter-token string@>=
  8037. begin
  8038. if (use_default) then
  8039.     begin
  8040.     if (not double_letter) then
  8041.     append_ex_buf_char_and_check (period);
  8042.     if (lex_class[name_sep_char[cur_token]] = sep_char) then
  8043.     append_ex_buf_char_and_check (name_sep_char[cur_token])
  8044.     else if ((cur_token = last_token-1) or
  8045.             (not enough_text_chars (long_token))) then
  8046.     append_ex_buf_char_and_check (tie)
  8047.     else
  8048.     append_ex_buf_char_and_check (space);
  8049.     end
  8050.   else
  8051.     begin
  8052.     if (ex_buf_length+(sp_xptr2-sp_xptr1) > buf_size) then
  8053.     buffer_overflow;
  8054.     sp_ptr := sp_xptr1;
  8055.     while (sp_ptr < sp_xptr2) do
  8056.     begin
  8057.     append_ex_buf_char (str_pool[sp_ptr]);
  8058.     incr(sp_ptr);
  8059.     end;
  8060. @^special character@>
  8061. This function looks at the string in |ex_buf|, starting at
  8062. |ex_buf_xptr| and ending just before |ex_buf_ptr|, and it returns
  8063. |true| if there are |enough_chars|, where a special character (even if
  8064. it's missing its matching |right_brace|) counts as a single charcter.
  8065. This procedure is called only for strings that don't have too many
  8066. |right_brace|s.
  8067. @<Procedures and functions for name-string processing@>=
  8068. function enough_text_chars (@!enough_chars : buf_pointer) : boolean;
  8069. begin
  8070. num_text_chars := 0;
  8071. ex_buf_yptr := ex_buf_xptr;
  8072. while ((ex_buf_yptr < ex_buf_ptr) and (num_text_chars < enough_chars)) do
  8073.     begin
  8074.     incr(ex_buf_yptr);
  8075.     if (ex_buf[ex_buf_yptr-1] = left_brace) then
  8076.     begin
  8077.     incr(brace_level);
  8078.     if ((brace_level = 1) and (ex_buf_yptr < ex_buf_ptr)) then
  8079.       if (ex_buf[ex_buf_yptr] = backslash) then
  8080.         begin
  8081.         incr(ex_buf_yptr);            {skip over the |backslash|}
  8082.         while ((ex_buf_yptr < ex_buf_ptr) and (brace_level > 0)) do
  8083.         begin
  8084.         if (ex_buf[ex_buf_yptr] = right_brace) then
  8085.             decr(brace_level)
  8086.         else if (ex_buf[ex_buf_yptr] = left_brace) then
  8087.             incr(brace_level);
  8088.         incr(ex_buf_yptr);
  8089.         end;
  8090.         end;
  8091.     else if (ex_buf[ex_buf_yptr-1] = right_brace) then
  8092.     decr(brace_level);
  8093.     incr(num_text_chars);
  8094.     end;
  8095. if (num_text_chars < enough_chars) then
  8096.     enough_text_chars := false
  8097.   else
  8098.     enough_text_chars := true;
  8099. If the last character output for this name part is a |tie| but the
  8100. previous character it isn't, we're dealing with a discretionary |tie|;
  8101. thus we replace it by a |space| if there are enough characters in the
  8102. rest of the name part.
  8103. @d long_name = 3        {a name this length or longer is ``long''}
  8104. @<Handle a discretionary |tie|@>=
  8105. begin
  8106. decr(ex_buf_ptr);            {remove the previous |tie|}
  8107. if (ex_buf[ex_buf_ptr-1] = tie) then    {it's not a discretionary |tie|}
  8108.     do_nothing
  8109. else if    (not enough_text_chars (long_name)) then {this is a short name part}
  8110.     incr(ex_buf_ptr)            {so restore the |tie|}
  8111. else                    {replace it by a |space|}
  8112.     append_ex_buf_char (space);
  8113. This is a procedure so that |x_format_name| is smaller.
  8114. @<Procedures and functions for name-string processing@>=
  8115. procedure figure_out_the_formatted_name;
  8116. label loop_exit;
  8117. begin
  8118. @<Figure out the formatted name@>;
  8119. The |built_in| function {\.{if\$}} pops the top three literals (they
  8120. are two function literals and an integer literal, in that order); if
  8121. the integer is greater than 0, it executes the second literal, else it
  8122. executes the first.  If any of the types is incorrect, it complains
  8123. but does nothing else.
  8124. @<|execute_fn|({\.{if\$}})@>=
  8125. begin
  8126. pop_lit_stk (pop_lit1,pop_typ1);
  8127. pop_lit_stk (pop_lit2,pop_typ2);
  8128. pop_lit_stk (pop_lit3,pop_typ3);
  8129. if (pop_typ1 <> stk_fn) then
  8130.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_fn)
  8131. else if (pop_typ2 <> stk_fn) then
  8132.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_fn)
  8133. else if (pop_typ3 <> stk_int) then
  8134.     print_wrong_stk_lit (pop_lit3,pop_typ3,stk_int)
  8135.     if (pop_lit3 > 0) then
  8136.     execute_fn (pop_lit2)
  8137.       else
  8138.     execute_fn (pop_lit1);
  8139. The |built_in| function {\.{int.to.chr\$}} pops the top (integer)
  8140. literal, interpreted as the |ASCII_code| of a single character,
  8141. converts it to the corresponding single-character string, and pushes
  8142. this string.  If the literal isn't an appropriate integer, it
  8143. complains and pushes the null string.
  8144. @<|execute_fn|({\.{int.to.chr\$}})@>=
  8145. procedure x_int_to_chr;
  8146. begin
  8147. pop_lit_stk (pop_lit1,pop_typ1);
  8148. if (pop_typ1 <> stk_int) then
  8149.     begin
  8150.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8151.     push_lit_stk (s_null, stk_str);
  8152.     end
  8153. else if ((pop_lit1 < 0) or (pop_lit1 > 127)) then
  8154.     begin
  8155.     bst_ex_warn (pop_lit1:0,' isn''t valid ASCII');
  8156.     push_lit_stk (s_null, stk_str);
  8157.     end
  8158.     begin
  8159.     str_room(1);
  8160.     append_char (pop_lit1);
  8161.     push_lit_stk (make_string, stk_str);
  8162.     end;
  8163. The |built_in| function {\.{int.to.str\$}} pops the top (integer)
  8164. literal, converts it to its (unique) string equivalent, and pushes
  8165. this string.  If the literal isn't an integer, it complains and pushes
  8166. the null string.
  8167. @<|execute_fn|({\.{int.to.str\$}})@>=
  8168. procedure x_int_to_str;
  8169. begin
  8170. pop_lit_stk (pop_lit1,pop_typ1);
  8171. if (pop_typ1 <> stk_int) then
  8172.     begin
  8173.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8174.     push_lit_stk (s_null, stk_str);
  8175.     end
  8176.     begin
  8177.     int_to_ASCII (pop_lit1, ex_buf, 0, ex_buf_length);@/
  8178.     add_pool_buf_and_push;        {push this string onto the stack}
  8179.     end;
  8180. The |built_in| function {\.{missing\$}} pops the top literal and
  8181. pushes the integer 1 if it's a missing field, 0 otherwise.  If the
  8182. literal isn't a missing field or a string, it complains and pushes 0.
  8183. Unlike \.{empty\$}, this function should be called only when
  8184. |mess_with_entries| is true.
  8185. @<|execute_fn|({\.{missing\$}})@>=
  8186. procedure x_missing;
  8187. begin
  8188. pop_lit_stk (pop_lit1,pop_typ1);
  8189. if (not mess_with_entries) then
  8190.     bst_cant_mess_with_entries_print
  8191. else if ((pop_typ1 <> stk_str) and (pop_typ1 <> stk_field_missing)) then
  8192.     begin
  8193.     if (pop_typ1 <> stk_empty) then
  8194.     begin
  8195.     print_stk_lit (pop_lit1,pop_typ1);
  8196.     bst_ex_warn (', not a string or missing field,');
  8197.     end;
  8198.     push_lit_stk (0, stk_int);
  8199.     end
  8200.     if (pop_typ1 = stk_field_missing) then
  8201.     push_lit_stk (1, stk_int)
  8202.       else
  8203.     push_lit_stk (0, stk_int);
  8204. The |built_in| function {\.{newline\$}} writes whatever has
  8205. accumulated in the output buffer |out_buf| onto the \.{.bbl} file.
  8206. @<|execute_fn|({\.{newline\$}})@>=
  8207. begin
  8208. output_bbl_line;
  8209. The |built_in| function {\.{num.names\$}} pops the top (string)
  8210. literal; it pushes the number of names the string represents---one
  8211. plus the number of occurrences of the substring ``and'' (ignoring case
  8212. differences) surrounded by nonnull |white_space| at the top brace
  8213. level.  If the literal isn't a string, it complains and pushes the
  8214. value 0.
  8215. @<|execute_fn|({\.{num.names\$}})@>=
  8216. procedure x_num_names;
  8217. begin
  8218. pop_lit_stk (pop_lit1,pop_typ1);
  8219. if (pop_typ1 <> stk_str) then
  8220.     begin
  8221.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  8222.     push_lit_stk (0, stk_int);
  8223.     end
  8224.     begin
  8225.     ex_buf_length := 0;
  8226.     add_buf_pool (pop_lit1);
  8227.     @<Determine the number of names@>;
  8228.     push_lit_stk (num_names, stk_int);
  8229.     end;
  8230. This module, while scanning the list of names, counts the occurrences
  8231. of ``and'' (ignoring case differences) surrounded by nonnull
  8232. |white_space|, and adds 1.
  8233. @<Determine the number of names@>=
  8234. begin
  8235. ex_buf_ptr := 0;
  8236. num_names := 0;
  8237. while (ex_buf_ptr < ex_buf_length) do
  8238.     begin
  8239.     name_scan_for_and (pop_lit1);
  8240.     incr(num_names);
  8241.     end;
  8242. The |built_in| function {\.{pop\$}} pops the top of the stack but
  8243. doesn't print it.
  8244. @<|execute_fn|({\.{pop\$}})@>=
  8245. begin
  8246. pop_lit_stk (pop_lit1,pop_typ1);
  8247. The |built_in| function {\.{preamble\$}} pushes onto the stack the
  8248. concatenation of all the \.{preamble} strings read from the database
  8249. files.
  8250. @<|execute_fn|({\.{preamble\$}})@>=
  8251. procedure x_preamble;
  8252. begin
  8253. ex_buf_length := 0;
  8254. preamble_ptr := 0;
  8255. while (preamble_ptr < num_preamble_strings) do
  8256.     begin
  8257.     add_buf_pool (s_preamble[preamble_ptr]);
  8258.     incr(preamble_ptr);
  8259.     end;
  8260. add_pool_buf_and_push;        {push the concatenation string onto the stack}
  8261. @^special character@>
  8262. The |built_in| function {\.{purify\$}} pops the top (string) literal,
  8263. removes nonalphanumeric characters except for |white_space| and
  8264. |sep_char| characters (these get converted to a |space|) and removes
  8265. certain alphabetic characters contained in the control sequences
  8266. associated with a special character, and pushes the resulting string.
  8267. If the literal isn't a string, it complains and pushes the null
  8268. string.
  8269. @<|execute_fn|({\.{purify\$}})@>=
  8270. procedure x_purify;
  8271. begin
  8272. pop_lit_stk (pop_lit1,pop_typ1);
  8273. if (pop_typ1 <> stk_str) then
  8274.     begin
  8275.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  8276.     push_lit_stk (s_null, stk_str);
  8277.     end
  8278.     begin
  8279.     ex_buf_length := 0;
  8280.     add_buf_pool (pop_lit1);
  8281.     @<Perform the purification@>;
  8282.     add_pool_buf_and_push;        {push this string onto the stack}
  8283.     end;
  8284. @^special character@>
  8285. The resulting string has nonalphanumeric characters removed, and each
  8286. |white_space| or |sep_char| character converted to a |space|.  The next
  8287. module handles special characters.  This code doesn't complain if the
  8288. string isn't brace balanced.
  8289. @<Perform the purification@>=
  8290. begin
  8291. brace_level := 0;    {this is the top level}
  8292. ex_buf_xptr := 0;    {this pointer is for the purified string}
  8293. ex_buf_ptr := 0;    {and this one is for the original string}
  8294. while (ex_buf_ptr < ex_buf_length) do
  8295.     begin
  8296.     case (lex_class[ex_buf[ex_buf_ptr]]) of
  8297.     white_space,
  8298.     sep_char :
  8299.         begin
  8300.         ex_buf[ex_buf_xptr] := space;
  8301.         incr(ex_buf_xptr);
  8302.         end;
  8303.     alpha,
  8304.     numeric :
  8305.         begin
  8306.         ex_buf[ex_buf_xptr] := ex_buf[ex_buf_ptr];
  8307.         incr(ex_buf_xptr);
  8308.         end;
  8309.     othercases
  8310.         if (ex_buf[ex_buf_ptr] = left_brace) then
  8311.         begin
  8312.         incr(brace_level);
  8313.         if ((brace_level = 1) and
  8314.                 (ex_buf_ptr + 1 < ex_buf_length)) then
  8315.           if (ex_buf[ex_buf_ptr+1] = backslash) then
  8316.             @<Purify a special character@>;
  8317.         end
  8318.         else if (ex_buf[ex_buf_ptr] = right_brace) then
  8319.         if (brace_level > 0) then
  8320.             decr(brace_level)
  8321.     endcases;
  8322.     incr(ex_buf_ptr);
  8323.     end;
  8324. ex_buf_length := ex_buf_xptr;
  8325. @^special character@>
  8326. Special characters (even without a matching |right_brace|) are
  8327. purified by removing the control sequences (but restoring the correct
  8328. thing for `\.{\\i}' and `\.{\\j}' as well as the eleven alphabetic
  8329. foreign characters in Table~3.2 of the \LaTeX\ manual) and removing
  8330. all nonalphanumeric characters (including |white_space| and
  8331. |sep_char|s).
  8332. @<Purify a special character@>=
  8333. begin
  8334. incr(ex_buf_ptr);            {skip over the |left_brace|}
  8335. while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
  8336.     begin
  8337.     incr(ex_buf_ptr);            {skip over the |backslash|}
  8338.     ex_buf_yptr := ex_buf_ptr;    {mark the beginning of the control sequence}
  8339.     while ((ex_buf_ptr < ex_buf_length) and
  8340.         (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do@/
  8341.     incr(ex_buf_ptr);        {this scans the control sequence}
  8342.     control_seq_loc := str_lookup(ex_buf,ex_buf_yptr,ex_buf_ptr-ex_buf_yptr,
  8343.                         control_seq_ilk,dont_insert);
  8344.     if (hash_found) then
  8345.     @<Purify this accented or foreign character@>;
  8346.     while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
  8347.                     (ex_buf[ex_buf_ptr] <> backslash)) do
  8348.     begin            {this scans to the next control sequence}
  8349.     case (lex_class[ex_buf[ex_buf_ptr]]) of
  8350.         alpha,
  8351.         numeric :
  8352.         begin
  8353.         ex_buf[ex_buf_xptr] := ex_buf[ex_buf_ptr];
  8354.         incr(ex_buf_xptr);
  8355.         end;
  8356.         othercases
  8357.         if (ex_buf[ex_buf_ptr] = right_brace) then
  8358.             decr(brace_level)
  8359.         else if (ex_buf[ex_buf_ptr] = left_brace) then
  8360.             incr(brace_level)
  8361.     endcases;
  8362.     incr(ex_buf_ptr);
  8363.     end;
  8364.     end;
  8365. decr(ex_buf_ptr);        {unskip the |right_brace| (or last character)}
  8366. We consider the purified character to be either the first alphabetic
  8367. character of its control sequence, or perhaps both alphabetic
  8368. characters.
  8369. @<Purify this accented or foreign character@>=
  8370. begin
  8371. ex_buf[ex_buf_xptr] := ex_buf[ex_buf_yptr]; {the first alphabetic character}
  8372. incr(ex_buf_xptr);
  8373. case (ilk_info[control_seq_loc]) of
  8374.     n_oe,
  8375.     n_oe_upper,
  8376.     n_ae,
  8377.     n_ae_upper,
  8378.     n_ss :
  8379.     begin                    {and the second}
  8380.     ex_buf[ex_buf_xptr] := ex_buf[ex_buf_yptr+1];
  8381.     incr(ex_buf_xptr);
  8382.     end;
  8383.     othercases
  8384.     do_nothing
  8385. endcases;
  8386. The |built_in| function {\.{quote\$}} pushes the string consisting of
  8387. the |double_quote| character.
  8388. @<|execute_fn|({\.{quote\$}})@>=
  8389. procedure x_quote;
  8390. begin
  8391. str_room(1);
  8392. append_char (double_quote);
  8393. push_lit_stk (make_string, stk_str);
  8394. The |built_in| function {\.{skip\$}} is a no-op.
  8395. @<|execute_fn|({\.{skip\$}})@>=
  8396. begin
  8397. do_nothing;
  8398. The |built_in| function {\.{stack\$}} pops and prints the whole stack;
  8399. it's meant to be used for style designers while debugging.
  8400. @<|execute_fn|({\.{stack\$}})@>=
  8401. begin
  8402. pop_whole_stack;
  8403. @^push the literal stack@>
  8404. The |built_in| function {\.{substring\$}} pops the top three literals
  8405. (they are the two integers literals |pop_lit1| and |pop_lit2| and a
  8406. string literal, in that order).  It pushes the substring of the (at
  8407. most) |pop_lit1| consecutive characters starting at the |pop_lit2|th
  8408. character (assuming 1-based indexing) if |pop_lit2| is positive, and
  8409. ending at the |-pop_lit2|th character from the end if |pop_lit2| is
  8410. negative (where the first character from the end is the last
  8411. character).  If any of the types is incorrect, it complain and pushes
  8412. the null string.
  8413. @<|execute_fn|({\.{substring\$}})@>=
  8414. procedure x_substring;
  8415. label exit;
  8416. begin
  8417. pop_lit_stk (pop_lit1,pop_typ1);
  8418. pop_lit_stk (pop_lit2,pop_typ2);
  8419. pop_lit_stk (pop_lit3,pop_typ3);
  8420. if (pop_typ1 <> stk_int) then
  8421.     begin
  8422.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8423.     push_lit_stk (s_null, stk_str);
  8424.     end
  8425. else if (pop_typ2 <> stk_int) then
  8426.     begin
  8427.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  8428.     push_lit_stk (s_null, stk_str);
  8429.     end
  8430. else if (pop_typ3 <> stk_str) then
  8431.     begin
  8432.     print_wrong_stk_lit (pop_lit3,pop_typ3,stk_str);
  8433.     push_lit_stk (s_null, stk_str);
  8434.     end
  8435.     begin
  8436.     sp_length := length(pop_lit3);
  8437.     if (pop_lit1 >= sp_length) then
  8438.       if ((pop_lit2 = 1) or (pop_lit2 = -1)) then
  8439.     begin
  8440.     repush_string;
  8441.     return;
  8442.     end;
  8443.     if ((pop_lit1 <= 0) or (pop_lit2 = 0) or (pop_lit2 > sp_length) or
  8444.                     (pop_lit2 < -sp_length)) then
  8445.     begin
  8446.     push_lit_stk (s_null, stk_str);
  8447.     return;
  8448.       else
  8449.     @<Form the appropriate substring@>;
  8450.     end;
  8451. exit:
  8452. @^push the literal stack@>
  8453. This module finds the substring as described in the last section,
  8454. and slides it into place in the string pool, if necessary.
  8455. @<Form the appropriate substring@>=
  8456. begin
  8457. if (pop_lit2 > 0) then
  8458.     begin
  8459.     if (pop_lit1 > sp_length - (pop_lit2-1)) then
  8460.     pop_lit1 := sp_length - (pop_lit2-1);
  8461.     sp_ptr := str_start[pop_lit3] + (pop_lit2-1);
  8462.     sp_end := sp_ptr + pop_lit1;
  8463.     if (pop_lit2 = 1) then
  8464.       if (pop_lit3 >= cmd_str_ptr) then    {no shifting---merely change pointers}
  8465.     begin
  8466.     str_start[pop_lit3+1] := sp_end;
  8467.     unflush_string;
  8468.     incr(lit_stk_ptr);
  8469.     return;
  8470.     end;
  8471.     end
  8472. else                    {|-ex_buf_length <= pop_lit2 < 0|}
  8473.     begin
  8474.     pop_lit2 := -pop_lit2;
  8475.     if (pop_lit1 > sp_length - (pop_lit2-1)) then
  8476.     pop_lit1 := sp_length - (pop_lit2-1);
  8477.     sp_end := str_start[pop_lit3+1] - (pop_lit2-1);
  8478.     sp_ptr := sp_end - pop_lit1;
  8479.     end;
  8480. while (sp_ptr < sp_end) do            {shift the substring}
  8481.     begin
  8482.     append_char (str_pool[sp_ptr]);
  8483.     incr(sp_ptr);
  8484.     end;
  8485. push_lit_stk (make_string, stk_str);        {and push it onto the stack}
  8486. The |built_in| function {\.{swap\$}} pops the top two literals from
  8487. the stack and pushes them back swapped.
  8488. @<|execute_fn|({\.{swap\$}})@>=
  8489. procedure x_swap;
  8490. begin
  8491. pop_lit_stk (pop_lit1,pop_typ1);
  8492. pop_lit_stk (pop_lit2,pop_typ2);
  8493. if ((pop_typ1 <> stk_str) or (pop_lit1 < cmd_str_ptr)) then
  8494.     begin
  8495.     push_lit_stk (pop_lit1, pop_typ1);
  8496.     if ((pop_typ2 = stk_str) and (pop_lit2 >= cmd_str_ptr)) then
  8497.     unflush_string;
  8498.     push_lit_stk (pop_lit2, pop_typ2);
  8499.     end
  8500. else if ((pop_typ2 <> stk_str) or (pop_lit2 < cmd_str_ptr)) then
  8501.     begin
  8502.     unflush_string;            {this is |pop_lit1|}
  8503.     push_lit_stk (pop_lit1, stk_str);
  8504.     push_lit_stk (pop_lit2, pop_typ2);
  8505.     end
  8506. else                    {bummer, both are recent strings}
  8507.     @<Swap the two strings (they're at the end of |str_pool|)@>;
  8508. We have to swap both (a)~the strings at the end of the string pool,
  8509. and (b)~their pointers on the literal stack.
  8510. @<Swap the two strings (they're at the end of |str_pool|)@>=
  8511. begin
  8512. ex_buf_length := 0;
  8513. add_buf_pool (pop_lit2);        {save the second string}
  8514. sp_ptr := str_start[pop_lit1];
  8515. sp_end := str_start[pop_lit1+1];
  8516. while (sp_ptr < sp_end) do        {slide the first string down}
  8517.     begin
  8518.     append_char (str_pool[sp_ptr]);
  8519.     incr(sp_ptr);
  8520.     end;
  8521. push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  8522. add_pool_buf_and_push;            {push second string onto the stack}
  8523. @^special character@>
  8524. The |built_in| function {\.{text.length\$}} pops the top (string)
  8525. literal, and pushes the number of text characters it contains, where
  8526. an accented character (more precisely, a ``special character''$\!$,
  8527. defined earlier) counts as a single text character, even if it's
  8528. missing its matching |right_brace|, and where braces don't count as
  8529. text characters.  If the literal isn't a string, it complains and
  8530. pushes the null string.
  8531. @<|execute_fn|({\.{text.length\$}})@>=
  8532. procedure x_text_length;
  8533. begin
  8534. pop_lit_stk (pop_lit1,pop_typ1);
  8535. if (pop_typ1 <> stk_str) then
  8536.     begin
  8537.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  8538.     push_lit_stk (s_null, stk_str);
  8539.     end
  8540.     begin
  8541.     num_text_chars := 0;
  8542.     @<Count the text characters@>;
  8543.     push_lit_stk (num_text_chars, stk_int);    {and push it onto the stack}
  8544.     end;
  8545. @^special character@>
  8546. Here we determine the number of text characters in the string, where
  8547. an entire special character counts as a single text character (even if
  8548. it's missing its matching |right_brace|), and where braces don't count
  8549. as text characters.
  8550. @<Count the text characters@>=
  8551. begin
  8552. sp_ptr := str_start[pop_lit1];
  8553. sp_end := str_start[pop_lit1+1];
  8554. sp_brace_level := 0;
  8555. while (sp_ptr < sp_end) do
  8556.     begin
  8557.     incr(sp_ptr);
  8558.     if (str_pool[sp_ptr-1] = left_brace) then
  8559.     begin
  8560.     incr(sp_brace_level);
  8561.     if ((sp_brace_level = 1) and (sp_ptr < sp_end)) then
  8562.       if (str_pool[sp_ptr] = backslash) then
  8563.         begin
  8564.         incr(sp_ptr);        {skip over the |backslash|}
  8565.         while ((sp_ptr < sp_end) and (sp_brace_level > 0)) do
  8566.         begin
  8567.         if (str_pool[sp_ptr] = right_brace) then
  8568.             decr(sp_brace_level)
  8569.         else if (str_pool[sp_ptr] = left_brace) then
  8570.             incr(sp_brace_level);
  8571.         incr(sp_ptr);
  8572.         end;
  8573.         incr(num_text_chars);
  8574.         end;
  8575.     else if (str_pool[sp_ptr-1] = right_brace) then
  8576.     begin
  8577.     if (sp_brace_level > 0) then
  8578.         decr(sp_brace_level);
  8579.     else
  8580.     incr(num_text_chars);
  8581.     end;
  8582. @^special character@>
  8583. The |built_in| function {\.{text.prefix\$}} pops the top two literals
  8584. (the integer literal |pop_lit1| and a string literal, in that order).
  8585. It pushes the substring of the (at most) |pop_lit1| consecutive text
  8586. characters starting from the beginning of the string.  This function
  8587. is similar to {\.{substring\$}}, but this one considers an accented
  8588. character (or more precisely, a ``special character''$\!$, even if
  8589. it's missing its matching |right_brace|) to be a single text character
  8590. (rather than however many |ASCII_code| characters it actually
  8591. comprises), and this function doesn't consider braces to be text
  8592. characters; furthermore, this function appends any needed matching
  8593. |right_brace|s.  If any of the types is incorrect, it complains and
  8594. pushes the null string.
  8595. @<|execute_fn|({\.{text.prefix\$}})@>=
  8596. procedure x_text_prefix;
  8597. label exit;
  8598. begin
  8599. pop_lit_stk (pop_lit1,pop_typ1);
  8600. pop_lit_stk (pop_lit2,pop_typ2);
  8601. if (pop_typ1 <> stk_int) then
  8602.     begin
  8603.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8604.     push_lit_stk (s_null, stk_str);
  8605.     end
  8606. else if (pop_typ2 <> stk_str) then
  8607.     begin
  8608.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
  8609.     push_lit_stk (s_null, stk_str);
  8610.     end
  8611. else if (pop_lit1 <= 0) then
  8612.     begin
  8613.     push_lit_stk (s_null, stk_str);
  8614.     return;
  8615.     end
  8616.     @<Form the appropriate prefix@>;
  8617. exit:
  8618. @^push the literal stack@>
  8619. This module finds the prefix as described in the last section, and
  8620. appends any needed matching |right_brace|s.
  8621. @<Form the appropriate prefix@>=
  8622. begin
  8623. sp_ptr := str_start[pop_lit2];
  8624. sp_end := str_start[pop_lit2+1];    {this may change}
  8625. @<Scan the appropriate number of characters@>;
  8626. if (pop_lit2 >= cmd_str_ptr) then    {no shifting---merely change pointers}
  8627.     pool_ptr := sp_end
  8628.     while (sp_ptr < sp_end) do        {shift the substring}
  8629.     begin
  8630.     append_char (str_pool[sp_ptr]);
  8631.     incr(sp_ptr);
  8632.     end;
  8633. while (sp_brace_level > 0) do        {add matching |right_brace|s}
  8634.     begin
  8635.     append_char (right_brace);
  8636.     decr(sp_brace_level);
  8637.     end;
  8638. push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  8639. @^special character@>
  8640. This section scans |pop_lit1| text characters, where an entire special
  8641. character counts as a single text character (even if it's missing its
  8642. matching |right_brace|), and where braces don't count as text
  8643. characters.
  8644. @<Scan the appropriate number of characters@>=
  8645. begin
  8646. num_text_chars := 0;
  8647. sp_brace_level := 0;
  8648. sp_xptr1 := sp_ptr;
  8649. while ((sp_xptr1 < sp_end) and (num_text_chars < pop_lit1)) do
  8650.     begin
  8651.     incr(sp_xptr1);
  8652.     if (str_pool[sp_xptr1-1] = left_brace) then
  8653.     begin
  8654.     incr(sp_brace_level);
  8655.     if ((sp_brace_level = 1) and (sp_xptr1 < sp_end)) then
  8656.       if (str_pool[sp_xptr1] = backslash) then
  8657.         begin
  8658.         incr(sp_xptr1);        {skip over the |backslash|}
  8659.         while ((sp_xptr1 < sp_end) and (sp_brace_level > 0)) do
  8660.         begin
  8661.         if (str_pool[sp_xptr1] = right_brace) then
  8662.             decr(sp_brace_level)
  8663.         else if (str_pool[sp_xptr1] = left_brace) then
  8664.             incr(sp_brace_level);
  8665.         incr(sp_xptr1);
  8666.         end;
  8667.         incr(num_text_chars);
  8668.         end;
  8669.     else if (str_pool[sp_xptr1-1] = right_brace) then
  8670.     begin
  8671.     if (sp_brace_level > 0) then
  8672.         decr(sp_brace_level);
  8673.     else
  8674.     incr(num_text_chars);
  8675.     end;
  8676. sp_end := sp_xptr1;
  8677. The |built_in| function {\.{top\$}} pops and prints the top of the
  8678. stack.
  8679. @<|execute_fn|({\.{top\$}})@>=
  8680. begin
  8681. pop_top_and_print;
  8682. The |built_in| function {\.{type\$}} pushes the appropriate string
  8683. from |type_list| onto the stack (unless either it's |undefined| or
  8684. |empty|, in which case it pushes the null string).
  8685. @<|execute_fn|({\.{type\$}})@>=
  8686. procedure x_type;
  8687. begin
  8688. if (not mess_with_entries) then
  8689.     bst_cant_mess_with_entries_print
  8690.   else
  8691.     if ((type_list[cite_ptr] = undefined) or
  8692.                 (type_list[cite_ptr] = empty)) then
  8693.     push_lit_stk (s_null, stk_str)
  8694.       else
  8695.     push_lit_stk (hash_text[type_list[cite_ptr]], stk_str);
  8696. The |built_in| function {\.{warning\$}} pops the top (string) literal
  8697. and prints it following a warning message.  This is implemented as a
  8698. special |built_in| function rather than using the {\.{top\$}} function
  8699. so that it can |mark_warning|.
  8700. @<|execute_fn|({\.{warning\$}})@>=
  8701. procedure x_warning;
  8702. begin
  8703. pop_lit_stk (pop_lit1,pop_typ1);
  8704. if (pop_typ1 <> stk_str) then
  8705.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str)
  8706.     begin
  8707.     print ('Warning--');
  8708.     print_lit (pop_lit1,pop_typ1);
  8709.     mark_warning;
  8710.     end;
  8711. The |built_in| function {\.{while\$}} pops the top two (function)
  8712. literals, and keeps executing the second as long as the (integer)
  8713. value left on the stack by executing the first is greater than 0.  If
  8714. either type is incorrect, it complains but does nothing else.
  8715. @<|execute_fn|({\.{while\$}})@>=
  8716. begin
  8717. pop_lit_stk (r_pop_lt1,r_pop_tp1);
  8718. pop_lit_stk (r_pop_lt2,r_pop_tp2);
  8719. if (r_pop_tp1 <> stk_fn) then
  8720.     print_wrong_stk_lit (r_pop_lt1,r_pop_tp1,stk_fn)
  8721. else if (r_pop_tp2 <> stk_fn) then
  8722.     print_wrong_stk_lit (r_pop_lt2,r_pop_tp2,stk_fn)
  8723.     loop
  8724.     begin
  8725.     execute_fn (r_pop_lt2);            {this is the \.{while\$} test}
  8726.     pop_lit_stk (pop_lit1,pop_typ1);
  8727.     if (pop_typ1 <> stk_int) then
  8728.         begin
  8729.         print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8730.         goto end_while;
  8731.         end
  8732.     else
  8733.         if (pop_lit1 > 0) then
  8734.         execute_fn (r_pop_lt1)        {this is the \.{while\$} body}
  8735.           else
  8736.         goto end_while;
  8737.     end;
  8738. end_while:    {justifies this |mean_while|}
  8739. @^literal literal@>
  8740. @^special character@>
  8741. The |built_in| function {\.{width\$}} pops the top (string) literal
  8742. and pushes the integer that represents its width in units specified by
  8743. the |char_width| array.  This function takes the literal literally;
  8744. that is, it assumes each character in the string is to be printed as
  8745. is, regardless of whether the character has a special meaning to \TeX,
  8746. except that special characters (even without their |right_brace|s) are
  8747. handled specially.  If the literal isn't a string, it complains and
  8748. pushes~0.
  8749. @<|execute_fn|({\.{width\$}})@>=
  8750. procedure x_width;
  8751. begin
  8752. pop_lit_stk (pop_lit1,pop_typ1);
  8753. if (pop_typ1 <> stk_str) then
  8754.     begin
  8755.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  8756.     push_lit_stk (0, stk_int);
  8757.     end
  8758.     begin
  8759.     ex_buf_length := 0;
  8760.     add_buf_pool (pop_lit1);
  8761.     string_width := 0;
  8762.     @<Add up the |char_width|s in this string@>;
  8763.     push_lit_stk (string_width, stk_int);
  8764.     end
  8765. We use the natural width for all but special characters, and we
  8766. complain if the string isn't brace-balanced.
  8767. @<Add up the |char_width|s in this string@>=
  8768. begin
  8769. brace_level := 0;            {we're at the top level}
  8770. ex_buf_ptr := 0;            {and the beginning of string}
  8771. while (ex_buf_ptr < ex_buf_length) do
  8772.     begin
  8773.     if (ex_buf[ex_buf_ptr] = left_brace) then
  8774.     begin
  8775.     incr(brace_level);
  8776.     if ((brace_level = 1) and (ex_buf_ptr + 1 < ex_buf_length)) then
  8777.         if (ex_buf[ex_buf_ptr+1] = backslash) then
  8778.         @<Determine the width of this special character@>
  8779.           else
  8780.         string_width := string_width + char_width[left_brace]
  8781.       else
  8782.         string_width := string_width + char_width[left_brace];
  8783.     else if (ex_buf[ex_buf_ptr] = right_brace) then
  8784.     begin
  8785.     decr_brace_level (pop_lit1);
  8786.     string_width := string_width + char_width[right_brace];
  8787.     else
  8788.     string_width := string_width + char_width[ex_buf[ex_buf_ptr]];
  8789.     incr(ex_buf_ptr);
  8790.     end;
  8791. check_brace_level (pop_lit1);
  8792. @^special character@>
  8793. We use the natural widths of all characters except that some
  8794. characters have no width: braces, control sequences (except for the
  8795. usual 13 accented and foreign characters, whose widths are given in
  8796. the next module), and |white_space| following control sequences (even
  8797. a null control sequence).
  8798. @<Determine the width of this special character@>=
  8799. begin
  8800. incr(ex_buf_ptr);                {skip over the |left_brace|}
  8801. while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
  8802.     begin
  8803.     incr(ex_buf_ptr);            {skip over the |backslash|}
  8804.     ex_buf_xptr := ex_buf_ptr;
  8805.     while ((ex_buf_ptr < ex_buf_length) and
  8806.         (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do@/
  8807.     incr(ex_buf_ptr);        {this scans the control sequence}
  8808.     if ((ex_buf_ptr < ex_buf_length) and (ex_buf_ptr = ex_buf_xptr)) then
  8809.     incr(ex_buf_ptr)        {this skips a nonalpha control seq}
  8810.       else
  8811.     begin
  8812.     control_seq_loc := str_lookup(ex_buf,ex_buf_xptr,
  8813.             ex_buf_ptr-ex_buf_xptr,control_seq_ilk,dont_insert);
  8814.     if (hash_found) then
  8815.         @<Determine the width of this accented or foreign character@>;
  8816.     end;
  8817.     while ((ex_buf_ptr < ex_buf_length) and
  8818.         (lex_class[ex_buf[ex_buf_ptr]] = white_space)) do
  8819.     incr(ex_buf_ptr);            {this skips following |white_space|}
  8820.     while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
  8821.                     (ex_buf[ex_buf_ptr] <> backslash)) do
  8822.     begin            {this scans to the next control sequence}
  8823.     if (ex_buf[ex_buf_ptr] = right_brace) then
  8824.         decr(brace_level)
  8825.     else if (ex_buf[ex_buf_ptr] = left_brace) then
  8826.         incr(brace_level)
  8827.     else
  8828.         string_width := string_width + char_width[ex_buf[ex_buf_ptr]];
  8829.     incr(ex_buf_ptr);
  8830.     end;
  8831.     end;
  8832. decr(ex_buf_ptr);            {unskip the |right_brace|}
  8833. Five of the 13 possibilities resort to special information not present
  8834. in the |char_width| array; the other eight simply use |char_width|'s
  8835. information for the first letter of the control sequence.
  8836. @<Determine the width of this accented or foreign character@>=
  8837. begin
  8838. case (ilk_info[control_seq_loc]) of
  8839.     n_ss : string_width := string_width + ss_width;
  8840.     n_ae : string_width := string_width + ae_width;
  8841.     n_oe : string_width := string_width + oe_width;
  8842.     n_ae_upper : string_width := string_width + upper_ae_width;
  8843.     n_oe_upper : string_width := string_width + upper_oe_width;
  8844.     othercases
  8845.     string_width := string_width + char_width[ex_buf[ex_buf_xptr]]
  8846. endcases;
  8847. The |built_in| function {\.{write\$}} pops the top (string) literal
  8848. and writes it onto the output buffer |out_buf| (which will result in
  8849. stuff being written onto the \.{.bbl} file if the buffer fills up).  If
  8850. the literal isn't a string, it complains but does nothing else.
  8851. @<|execute_fn|({\.{write\$}})@>=
  8852. procedure x_write;
  8853. begin
  8854. pop_lit_stk (pop_lit1,pop_typ1);
  8855. if (pop_typ1 <> stk_str) then
  8856.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str)
  8857.     add_out_pool (pop_lit1);
  8858. @* Cleaning up.
  8859. @^clich\'e-\`a-trois@>
  8860. @^fat lady@>
  8861. @^turn out lights@>
  8862. @^Yogi@>
  8863. This section does any last-minute printing and ends the program.
  8864. @<Clean up and leave@>=
  8865. begin
  8866. if ((read_performed) and (not reading_completed)) then
  8867.     begin
  8868.     print ('Aborted at line ',bib_line_num:0,' of file ');
  8869.     print_bib_name;
  8870.     end;
  8871. trace_and_stat_printing;
  8872. @<Print the job |history|@>;
  8873. a_close (log_file);
  8874. {turn out the lights, the fat lady has sung; it's over, Yogi}
  8875. Here we print |trace| and/or |stat| information, if desired.
  8876. @<Procedures and functions for all file I/O, error messages, and such@>=
  8877. procedure trace_and_stat_printing;
  8878. begin
  8879.   trace
  8880.   @<Print all \.{.bib}- and \.{.bst}-file information@>;
  8881.   @<Print all |cite_list| and entry information@>;
  8882.   @<Print the |wiz_defined| functions@>;
  8883.   @<Print the string pool@>;
  8884.   ecart@/
  8885.   stat
  8886.   @<Print usage statistics@>;
  8887.   tats@/
  8888. This prints information obtained from the \.{.aux} file about the
  8889. other files.
  8890. @<Print all \.{.bib}- and \.{.bst}-file information@>=
  8891. begin
  8892. if (num_bib_files = 1) then
  8893.     trace_pr_ln ('The 1 database file is')
  8894.   else
  8895.     trace_pr_ln ('The ',num_bib_files:0,' database files are');
  8896. if (num_bib_files = 0) then
  8897.     trace_pr_ln ('   undefined')
  8898.   else
  8899.     begin
  8900.     bib_ptr := 0;
  8901.     while (bib_ptr < num_bib_files) do
  8902.     begin
  8903.     trace_pr ('   ');
  8904.     trace_pr_pool_str (cur_bib_str);
  8905.     trace_pr_pool_str (s_bib_extension);
  8906.     trace_pr_newline;
  8907.     incr(bib_ptr);
  8908.     end;
  8909.     end;
  8910. trace_pr ('The style file is ');
  8911. if (bst_str = 0) then
  8912.     trace_pr_ln ('undefined')
  8913.   else
  8914.     begin
  8915.     trace_pr_pool_str (bst_str);
  8916.     trace_pr_pool_str (s_bst_extension);
  8917.     trace_pr_newline;
  8918.     end;
  8919. In entry-sorted order, this prints an entry's |cite_list| string and,
  8920. indirectly, its entry type and entry variables.
  8921. @<Print all |cite_list| and entry information@>=
  8922. begin
  8923. if (all_entries) then
  8924.     trace_pr ('all_marker=',all_marker:0,', ');
  8925. if (read_performed) then
  8926.     trace_pr_ln ('old_num_cites=',old_num_cites:0)
  8927.   else
  8928.     trace_pr_newline;
  8929. trace_pr ('The ',num_cites:0);
  8930. if (num_cites = 1) then
  8931.     trace_pr_ln (' entry:')
  8932.   else
  8933.     trace_pr_ln (' entries:');
  8934. if (num_cites = 0) then
  8935.     trace_pr_ln ('   undefined')
  8936.   else
  8937.     begin
  8938.     sort_cite_ptr := 0;
  8939.     while (sort_cite_ptr < num_cites) do
  8940.     begin
  8941.     if (not read_completed) then    {we didn't finish the \.{read} command}
  8942.         cite_ptr := sort_cite_ptr
  8943.       else
  8944.         cite_ptr := sorted_cites[sort_cite_ptr];
  8945.     trace_pr_pool_str (cur_cite_str);
  8946.     if (read_performed) then
  8947.         @<Print entry information@>
  8948.       else
  8949.         trace_pr_newline;
  8950.     incr(sort_cite_ptr);
  8951.     end;
  8952.     end;
  8953. This prints information gathered while reading the \.{.bst} and
  8954. \.{.bib} files.
  8955. @<Print entry information@>=
  8956. begin
  8957. trace_pr (', entry-type ');
  8958. if (type_list[cite_ptr] = undefined) then
  8959.     undefined : trace_pr ('unknown')
  8960. else if (type_list[cite_ptr] = empty) then
  8961.     trace_pr ('--- no type found')
  8962.     trace_pr_pool_str (hash_text[type_list[cite_ptr]]);
  8963. trace_pr_ln (', has entry strings');
  8964. @<Print entry strings@>;
  8965. trace_pr ('  has entry integers');
  8966. @<Print entry integers@>;
  8967. trace_pr_ln ('  and has fields');
  8968. @<Print fields@>;
  8969. This prints, for the current entry, the strings declared by the
  8970. \.{entry} command.
  8971. @<Print entry strings@>=
  8972. begin
  8973. if (num_ent_strs = 0) then
  8974.     trace_pr_ln ('    undefined')
  8975. else if (not read_completed) then
  8976.     trace_pr_ln ('    uninitialized')
  8977.     begin
  8978.     str_ent_ptr := cite_ptr * num_ent_strs;
  8979.     while (str_ent_ptr < (cite_ptr+1)*num_ent_strs) do
  8980.     begin
  8981.     ent_chr_ptr := 0;
  8982.     trace_pr ('    "');
  8983.     while (entry_strs[str_ent_ptr][ent_chr_ptr] <> end_of_string) do
  8984.         begin
  8985.         trace_pr (xchr[entry_strs[str_ent_ptr][ent_chr_ptr]]);
  8986.         incr(ent_chr_ptr);
  8987.         end;
  8988.     trace_pr_ln ('"');
  8989.     incr(str_ent_ptr);
  8990.     end;
  8991.     end;
  8992. This prints, for the current entry, the integers declared by the
  8993. \.{entry} command.
  8994. @<Print entry integers@>=
  8995. begin
  8996. if (num_ent_ints = 0) then
  8997.     trace_pr (' undefined')
  8998. else if (not read_completed) then
  8999.     trace_pr (' uninitialized')
  9000.     begin
  9001.     int_ent_ptr := cite_ptr*num_ent_ints;
  9002.     while (int_ent_ptr < (cite_ptr+1)*num_ent_ints) do
  9003.     begin
  9004.     trace_pr (' ',entry_ints[int_ent_ptr]:0);
  9005.     incr(int_ent_ptr);
  9006.     end;
  9007.     end;
  9008. trace_pr_newline;
  9009. This prints the fields stored for the current entry.
  9010. @<Print fields@>=
  9011. begin
  9012. if (not read_performed) then
  9013.     trace_pr_ln ('    uninitialized')
  9014.   else
  9015.     begin
  9016.     field_ptr := cite_ptr * num_fields;
  9017.     field_end_ptr := field_ptr + num_fields;
  9018.     no_fields := true;
  9019.     while (field_ptr < field_end_ptr) do
  9020.     begin
  9021.     if (field_info[field_ptr] <> missing) then
  9022.         begin
  9023.         trace_pr ('    "');
  9024.         trace_pr_pool_str (field_info[field_ptr]);
  9025.         trace_pr_ln ('"');
  9026.         no_fields := false;
  9027.         end;
  9028.     incr(field_ptr);
  9029.     end;
  9030.     if (no_fields) then
  9031.     trace_pr_ln ('    missing');
  9032.     end;
  9033. This gives all the |wiz_defined| functions that appeared in the
  9034. \.{.bst} file.
  9035. @<Print the |wiz_defined| functions@>=
  9036. begin
  9037. trace_pr_ln ('The wiz-defined functions are');
  9038. if (wiz_def_ptr = 0) then
  9039.     trace_pr_ln ('   nonexistent')
  9040.   else
  9041.     begin
  9042.     wiz_fn_ptr := 0;
  9043.     while (wiz_fn_ptr < wiz_def_ptr) do
  9044.     begin
  9045.     if (wiz_functions[wiz_fn_ptr] = end_of_def) then
  9046.         trace_pr_ln (wiz_fn_ptr:0,'--end-of-def--')
  9047.     else if (wiz_functions[wiz_fn_ptr] = quote_next_fn) then
  9048.         trace_pr (wiz_fn_ptr:0,'  quote_next_function    ')
  9049.     else
  9050.         begin
  9051.         trace_pr (wiz_fn_ptr:0,'  `');
  9052.         trace_pr_pool_str (hash_text[wiz_functions[wiz_fn_ptr]]);
  9053.         trace_pr_ln ('''');
  9054.         end;
  9055.     incr(wiz_fn_ptr);
  9056.     end;
  9057.    end;
  9058. This includes all the `static' strings (that is, those that are also
  9059. in the hash table), but none of the dynamic strings (that is, those
  9060. put on the stack while executing \.{.bst} commands).
  9061. @<Print the string pool@>=
  9062. begin
  9063. trace_pr_ln ('The string pool is');
  9064. str_num := 1;
  9065. while (str_num < str_ptr) do
  9066.     begin
  9067.     trace_pr (str_num:4, str_start[str_num]:6,' "');
  9068.     trace_pr_pool_str (str_num);
  9069.     trace_pr_ln ('"');
  9070.     incr(str_num);
  9071.     end;
  9072. @^statistics@>
  9073. These statistics can help determine how large some of the constants
  9074. should be and can tell how useful certain |built_in| functions are.
  9075. They are written to the same files as tracing information.
  9076. @d stat_pr == trace_pr
  9077. @d stat_pr_ln == trace_pr_ln
  9078. @d stat_pr_pool_str == trace_pr_pool_str
  9079. @<Print usage statistics@>=
  9080. begin
  9081. stat_pr ('You''ve used ',num_cites:0);
  9082. if (num_cites = 1) then
  9083.     stat_pr_ln (' entry,')
  9084.   else
  9085.     stat_pr_ln (' entries,');
  9086. stat_pr_ln ('            ',wiz_def_ptr:0,' wiz_defined-function locations,');
  9087. stat_pr_ln ('            ',str_ptr:0,' strings with ',str_start[str_ptr]:0,
  9088.                             ' characters,');
  9089. blt_in_ptr := 0;
  9090. total_ex_count := 0;
  9091. while (blt_in_ptr < num_blt_in_fns) do
  9092.     begin
  9093.     total_ex_count := total_ex_count + execution_count[blt_in_ptr];
  9094.     incr(blt_in_ptr);
  9095.     end;
  9096. stat_pr_ln ('and the built_in function-call counts, ', total_ex_count:0,
  9097.                             ' in all, are:');
  9098. blt_in_ptr := 0;
  9099. while (blt_in_ptr < num_blt_in_fns) do
  9100.     begin
  9101.     stat_pr_pool_str (hash_text[blt_in_loc[blt_in_ptr]]);
  9102.     stat_pr_ln (' -- ',execution_count[blt_in_ptr]:0);
  9103.     incr(blt_in_ptr);
  9104.     end;
  9105. @^bunk, history@>
  9106. @^system dependencies@>
  9107. @:this can't happen}{\quad History is bunk@>
  9108. Some implementations may wish to pass the |history| value to the
  9109. operating system so that it can be used to govern whether or not other
  9110. programs are started. Here we simply report the history to the user.
  9111. @<Print the job |history|@>=
  9112. case (history) of
  9113.     spotless : do_nothing;
  9114.     warning_message : begin
  9115.               if (err_count = 1) then
  9116.               print_ln ('(There was 1 warning)')
  9117.             else
  9118.               print_ln ('(There were ',err_count:0,' warnings)');
  9119.               end;
  9120.     error_message : begin
  9121.             if (err_count = 1) then
  9122.             print_ln ('(There was 1 error message)')
  9123.               else
  9124.             print_ln ('(There were ',err_count:0,
  9125.                             ' error messages)');
  9126.             end;
  9127.     fatal_message : print_ln ('(That was a fatal error)');
  9128.     othercases begin
  9129.            print ('History is bunk');
  9130.            print_confusion;
  9131.            end
  9132. endcases
  9133. @* System-dependent changes.
  9134. @^system dependencies@>
  9135. This section should be replaced, if necessary, by changes to the program
  9136. that are necessary to make \BibTeX\ work at a particular installation.
  9137. It is usually best to design your change file so that all changes to
  9138. previous sections preserve the section numbering; then everybody's version
  9139. will be consistent with the printed program. More extensive changes,
  9140. which introduce new sections, can be inserted here; then only the index
  9141. itself will get a new section number.
  9142. @* Index.
  9143. @.this can't happen@>
  9144. Here is where you can find all uses of each identifier in the program,
  9145. with underlined entries pointing to where the identifier was defined.
  9146. If the identifier is only one letter long, however, you get to see only
  9147. the underlined entries. All references are to section numbers instead of
  9148. page numbers.
  9149. This index also lists a few error messages and other aspects of the
  9150. program that you might want to look up some day. For example, the
  9151. entry for ``system dependencies'' lists all sections that should
  9152. receive special attention from people who are installing \TeX\ in a
  9153. new operating environment. A list of various things that can't happen
  9154. appears under ``this can't happen''$\!$.
  9155.